C# Interview Questions for Freshers and Experienced 

  • By Anil Giri
  • March 5, 2024
  • C and C++ Programing
C# Interview Questions for Freshers and Experienced

C# Interview Questions for Freshers and Experienced 

Prepare for success in C# interviews with our comprehensive guide to common C# interview questions for freshers and experienced professionals.

What is C#? 

C sharp is an object-oriented programming language developed by Microsoft. C# is used with the .NET framework for creating websites, applications, and games. C# has a  varied reasons for being popular: 

  • Comparatively easier: Starting with C# is termed comparatively easier than other  programming languages 
  • Wider use of development: Using C#, you are prone to create web applications or gaming apps. C# has some fantastic features like automatic garbage collection,  interfaces, etc. which help build better applications. 
  • Larger target audience: Collaboration with Microsoft provides an edge to the applications created using C# since it would have a wider target audience. 

C# Interview Questions for Freshers

C# Interview Questions for Freshers 

  1. How is C# different from C? 

You would always know C is the procedural language while C# is a more object-oriented language. The biggest difference is that C# supports automatic garbage collection by Common Language Runtime (CLR) while C does not. C# primarily needs a .NET framework to execute while C is a platform-agnostic language.

  1. What is Common Language Runtime (CLR)?

CLR handles program execution for various languages including C#. The architecture of  CLR handles memory management, garbage collection, and security handling, and looks  like this: 

  1. What is garbage collection in C#?

Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the heap memory. Now, after you perform all the actions on the object,  the memory space occupied by the object becomes waste. It is necessary to free up memory. Garbage collection happens in three cases: 

  • If the occupied memory by the objects exceeds the pre-set threshold value. • If the garbage collection method is called 
  • If your system has low physical memory.

 

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

 

  1. What are the types of classes in C#?

Class is an entity that encapsulates all the properties of its objects and instances as a single unit. C# has four types of such classes:

  • Static class: Static class, defined by the keyword ‘static’ does not allow inheritance. Therefore, you cannot create an object for a static class. 

static class classname 

 //static data members 

 //static methods 

  • Partial class: Partial class, defined by the keyword ‘partial’ allows its members to partially divide or share source (.cs) files.
  • Abstract class: Abstract classes are classes that cannot be instantiated where you cannot create objects. Abstract classes work on the OOPS concept of abstraction. Abstraction helps to extract essential details and hide the  unessential ones. 
  • Sealed class: Sealed classes are classes that cannot be inherited. Use the keyword sealed to restrict access to users to inherit that class.

sealed class Test 

 // data members 

 // methods 

 . 

 . 

 . 

  1. What is a managed and unmanaged code?

Managed code lets you run the code on a managed CLR runtime environment in the  .NET framework. 

Managed code runs on the managed runtime environment rather than the operating system itself. 

Benefits: Provides various services like a garbage collector, exception handling, etc. 

Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that works outside the .NET framework. 

They don’t provide services of high-level languages and therefore, run without them. Such an example is C++.

 

  1. What is the difference between an abstract class and an interface? Let’s dig into the differences between an abstract class and an interface: 
  • Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods. 
  • Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods. 
  • Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface,  there is no such need. 

An abstract class has constructors while an interface encompasses none. Ex. 

Abstract class: 

public abstract class Shape

public abstract void draw(); 

Interface: 

public interface Paintable

void paint(); 

  1. What are the differences between ref and out keywords?

C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword,  you need to explicitly mention ‘ref’. 

void Method(ref int refArgument) 

 refArgument = refArgument + 10; 

int number = 1; 

Method(ref number); 

Console.WriteLine(number); 

// Output: 11

C# out keywords pass arguments within methods and functions. 

The ‘out’ keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialized before it is passed. Here, The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument. 

public static string GetNextFeature(ref int id)  

{  

 string returnText = “Next-” + id.ToString();  

 id += 1;  

 return returnText;  

}  

public static string GetNextFeature(out int id)  

{  

 id = 1;  

 string returnText = “Next-” + id.ToString();  

 return returnText;  

}  

 

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

 

  1. What are Generics in C#?

In C# collections, defining any kind of object is termed okay which compromises C#’s basic rule of type-safety. Therefore, generics were included to type-safe the code by allowing re-use of the data processing algorithms. Generics in C# mean not linked to  

any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting objects. Generics are always defined inside angular brackets <>. To create  a generic class, this syntax is used: 

GenericList<float> list1 = new GenericList<float>(); 

GenericList<Features> list2 = new GenericList<Features>(); 

GenericList<Struct> list3 = new GenericList<Struct>(); 

Here, GenericList<float> is a generic class. In each of these instances of  GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument. By substituting the T, we have created three different type-safe using the same class.

 

  1. What is the difference between an Array and an ArrayList in C#?

An array is a collection of similar variables clubbed together under one common name.  While ArrayList is a collection of objects that can be indexed individually. With  ArrayList you can access a number of features like dynamic memory allocation, adding,  searching, and sorting items in the ArrayList. 

  • When declaring an array the size of the items is fixed therefore, the memory allocation is fixed. But with ArrayList, it can be increased or decreased dynamically. 
  • Array belongs to system.array namespace while ArrayList belongs to the  system.collection namespace. 
  • All items in an array are of the same datatype while all the items in an ArrayList  can be of the same or different data types. 
  • While arrays cannot accept null, ArrayList can accept null values. For ex.: 

// C# program to illustrate the ArrayList 

using System; 

using System.Collections; 

class Test

 // Main Method 

 public static void Main(string[] args) 

 { 

 // Create a list of strings 

 ArrayList al = new ArrayList(); 

 al.Add(“Rohit”); 

 al.Add(“MSD”); 

 al.Add(10); 

 al.Add(10.10); 

 // Iterate list element using foreach loop 

 foreach(var names in al) 

 { 

 Console.WriteLine(names); 

 } 

 } 

}

 

C# Interview Questions for Experienced 

  1. What is Boxing and Unboxing in C#? 

The two functions are used for typecasting the data types: 

Boxing: Boxing converts value type (int, char, etc.) to reference type (object) which is  an implicit conversion process using object value. 

Example: 

int num = 23; // 23 will assigned to num 

Object Obj = num; // Boxing 

Unboxing: Unboxing converts reference type (object) to value type (int, char, etc.)  using an explicit conversion process. 

Example: 

int num = 23; // value type is int and assigned value 23 

Object Obj = num; // Boxing 

int i = (int)Obj; // Unboxing 

What is C#? 

C sharp is an object-oriented programming language developed by Microsoft. C# is  used with the .NET framework for creating websites, applications, and games. C# has a  varied reasons for being popular: 

  • Comparatively easier: Starting with C# is termed comparatively easier than other  programming languages 
  • Wider use of development: Using C#, you are prone to create web applications or  gaming apps. C# has some fantastic features like automatic garbage collection,  interfaces, etc. which help build better applications. 
  • Larger target audience: Collaboration with Microsoft provides an edge to the  applications created using C# since it would have a wider target audience. 

Since C# is such a widely used programming language, a plethora of big and small  organizations base their products using it. So, prepare yourself with basic and advanced  level C# questions to ace the interviews. 

Let’s look at the following comprehensive set of C# Interview Questions and  Answers which have been categorised below:

 

  1. What is garbage collection in C#? 

Garbage collection is the process of freeing up memory that is captured by unwanted  objects. When you create a class object, automatically some memory space is allocated  to the object in the heap memory. Now, after you perform all the actions on the object,  the memory space occupied by the object becomes waste. It is necessary to free up  memory. Garbage collection happens in three cases: 

  • If the occupied memory by the objects exceeds the pre-set threshold value. • If the garbage collection method is called
  • If your system has low physical memory
  1. What are the types of classes in C#? 

Class is an entity that encapsulates all the properties of its objects and instances as a  single unit. C# has four types of such classes: 

  • Static class: Static class, defined by the keyword ‘static’ does not allow inheritance. Therefore, you cannot create an object for a static class.

 

Sample code: 

static class classname 

 //static data members 

 //static methods 

  • Partial class: Partial class, defined by the keyword ‘partial’ allows its members to partially divide or share source (.cs) files.
  • Abstract class: Abstract classes are classes that cannot be instantiated where you cannot create objects. Abstract classes work on the OOPS concept of abstraction. Abstraction helps to extract essential details and hide the  unessential ones. 
  • Sealed class: Sealed classes are classes that cannot be inherited. Use the keyword sealed to restrict access to users to inherit that class.

sealed class Test 

 // data members 

 // methods 

 . 

 . 

 . 

  1. What is a managed and unmanaged code? 

Managed code lets you run the code on a managed CLR runtime environment in the  .NET framework. 

Managed code runs on the managed runtime environment than the operating system  itself. 

Benefits: Provides various services like a garbage collector, exception handling, etc. 

Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that works outside the .NET framework. 

They don’t provide services of the high-level languages and therefore, run without them. Such an example is C++. 

 

 

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

 

An abstract class has constructors while an interface encompasses none. Ex. 

Abstract class: 

public abstract class Shape

public abstract void draw(); 

Interface: 

public interface Paintable

void paint(); 

  1. What are the differences between ref and out keywords? 

C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword,  you need to explicitly mention ‘ref’. 

void Method(ref int refArgument) 

 refArgument = refArgument + 10; 

int number = 1; 

Method(ref number); 

Console.WriteLine(number); 

// Output: 11 

C# out keywords pass arguments within methods and functions. 

‘out’ keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialized before it is passed. Here, The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument.

public static string GetNextFeature(ref int id)  

{  

 string returnText = “Next-” + id.ToString();  

 id += 1;  

 return returnText;  

}  

public static string GetNextFeature(out int id)  

{  

 id = 1;  

 string returnText = “Next-” + id.ToString();  

 return returnText;  

}  

  1. What are extension methods in C#? 

Extension methods help to add new methods to the existing ones. The methods that  are added are static. At times, when you want to add methods to an existing class but  don’t perceive the right to modify that class or don’t hold the rights, you can create a  

new static class containing the new methods. Once the extended methods are  declared, bind this class with the existing one and see the methods will be added to the  existing one. 

// C# program to illustrate the concept 

// of the extension methods 

using System; 

namespace ExtensionMethod

static class NewMethodClass

 // Method 4 

 public static void M4(this Scaler s) 

 { 

 Console.WriteLine(“Method Name: M4”); 

 } 

 // Method 5 

 public static void M5(this Scaler s, string str) 

 { 

 Console.WriteLine(str); 

 } 

// Now we create a new class in which 

// Scaler class access all the five methods 

public class IB

 // Main Method 

 public static void Main(string[] args) 

 {

 Scaler s = new Scaler(); 

 s.M1(); 

 s.M2(); 

 s.M3(); 

 s.M4(); 

 s.M5(“Method Name: M5”); 

 } 

Output: 

Method Name: M1 

Method Name: M2 

Method Name: M3 

Method Name: M4 

Method Name: M5 

  1. What are Generics in C#? 

In C# collections, defining any kind of object is termed okay which compromises C#’s  basic rule of type-safety. Therefore, generics were included to type-safe the code by  allowing re-use of the data processing algorithms. Generics in C# mean not linked to  

any specific data type. Generics reduce the load of using boxing, unboxing, and  typecasting objects. Generics are always defined inside angular brackets <>. To create  a generic class, this syntax is used: 

GenericList<float> list1 = new GenericList<float>(); 

GenericList<Features> list2 = new GenericList<Features>(); 

GenericList<Struct> list3 = new GenericList<Struct>(); 

Here, GenericList<float> is a generic class. In each of these instances of  GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument. By substituting the T, we have created three different type-safe using the same class. 

  1. What is the difference between an Array and an ArrayList in C#? 

An array is a collection of similar variables clubbed together under one common name.  While ArrayList is a collection of objects that can be indexed individually. With  ArrayList you can access a number of features like dynamic memory allocation, adding,  searching, and sorting items in the ArrayList.

  • When declaring an array the size of the items is fixed therefore, the memory allocation is fixed. But with ArrayList, it can be increased or decreased dynamically. 
  • Array belongs to system.array namespace while ArrayList belongs to the system.collection namespace.
  • All items in an array are of the same datatype while all the items in an ArrayList can be of the same or different data types.
  • While arrays cannot accept null, ArrayList can accept null values. For ex.: 

// C# program to illustrate the ArrayList 

using System; 

using System.Collections; 

class IB

 // Main Method 

 public static void Main(string[] args) 

 { 

 // Create a list of strings 

 ArrayList al = new ArrayList(); 

 al.Add(“Bruno”); 

 al.Add(“Husky”); 

 al.Add(10); 

 al.Add(10.10); 

 // Iterate list element using foreach loop 

 foreach(var names in al) 

 { 

 Console.WriteLine(names); 

 } 

 } 

Also, C# doesn’t support multiple inheritances. 

Instead, you can use interfaces to inherit the properties using the class name in the signature. 

C# Interview Questions for Experienced 

  1. What are Properties in C#? 

Properties in C# are public members of a class where they provide the ability to access private members of a class. The basic principle of encapsulation lets you hide some sensitive properties from the users by making the variables private. The private members are not accessible otherwise in a class. Therefore, by using properties in C#  you can easily access the private members and set their values. 

The values can be easily assigned using get and set methods, also known as accessors.  While the get method extracts the value, the set method assigns the value to the variables. 

 

For Free, Demo classes Call: 8237077325

Registration Link: C and C++ Classes in Pune!

 

  1. What are partial classes in C#? 

Partial classes implement the functionality of a single class into multiple files. These multiple files are combined into one during compile time. The partial class can be created using the partial keyword. 

public partial Clas_name  

 // code

You can easily split the functionalities of methods, interfaces, or structures into multiple files. You can even add nested partial classes. 

  1. What is the difference between late binding and early binding in C#?

Late binding and early binding are examples of one of the primary concepts of  OOPS: Polymorphism. 

For ex: one function calculateBill() will calculate the bills of premium customers, basic customers, and semi-premium customers based on their policies differently. The calculation for all the customer objects is done differently using the same function which is called polymorphism. 

When an object is assigned to an object variable in C#, the .NET framework performs the binding. 

When the binding function happens at compile-time, it is called early binding. It investigates and checks the methods and properties of static objects. With early binding, the number of run-time errors decreases substantially and it executes pretty quickly. 

But when the binding happens at runtime, it is called late binding. Late binding happens when the objects are dynamic (decided based on the data they hold) at run-time. It is slower as it looks through during run-time. 

  1. What are the Arrays in C#?

When a group of similar elements is clubbed together under one name, they are called arrays. 

For ex. An array of tea Atea[4]: [green tea, chamomile tea, black tea, lemon tea]. The length of the array defines how many elements are present in the array. 

In C#, the memory allocations for the elements of the array happen dynamically. This is how values are stored in an array sequentially.

A few pointers for arrays in C#: 

  • The memory allocation is DYNAMIC. 
  • Arrays in C# are treated as objects. 
  • The length of the array is easy to find by detecting the number of members in the array. 
  • The members in the array are ordered and begin with the index value=0. • The array types are reference types derived from the base array type. 

Syntax: < Data Type > [ ] < Name_Array > 

  1. Difference between the Equality Operator (==) and Equals() Method in C#? 

Although both are used to compare two objects by value, still they are used differently. 

For ex.: 

int x = 10; 

int y = 10; 

Console.WriteLine( x == y); 

Console.WriteLine(x.Equals(y)); 

Output: 

True 

True 

Equality operator (==) is a reference type which means that if the equality operator is used,  it will return true only if both the references point to the same object. 

Equals() method: The Equals method is used to compare the values carried by the objects.  int x=10, int y=10. If x==y is compared then, the values carried by x and y are compared which is equal and therefore they return true. 

Equality operator: Compares by reference 

Equals(): Compares by value 

  1. What are the different ways in which a method can be Overloaded in C#?

Overloading means when a method has the same name but carries different values to use in a different context. Only the main() method cannot be overloaded. 

In order to overload methods in C#, 

Change the number of parameters in a method, or 

Change the order of parameters in a method, or 

Use different data types for parameters 

In these ways, you can overload a method multiple times. 

For ex. 

public class Area

 public double area(double x) { 

 double area = x * x; 

 return area; 

 } 

 public double area(double a, double b) { 

 double area = a * b; 

 return area; 

 } 

Here, the method Area is used twice. In the first declaration, one argument is used while in the  second one, there were two arguments are used. Using different parameters in the same method, we were able to overload the method area(). 

  1. What is the difference between String and StringBuilder in C#?

The major difference between String and StringBuilder is that String objects are immutable while StringBuilder creates a mutable string of characters. StringBuilder will make the changes to the existing object rather than creating a new object. 

StringBuilder simplifies the entire process of making changes to the existing string object. Since the String class is immutable, it is costlier to create a new object every time we need to make a change. So, the StringBuilder class comes into the picture which can be evoked using the System.Text namespace. 

In case, a string object will not change throughout the entire program, then use String class or else StringBuilder. 

For ex: 

string s = string.Empty;  

for (i = 0; i < 1000; i++)  

 {  

 s += i.ToString() + ” “;  

 } 

Here, you’ll need to create 2001 objects out of which 2000 will be of no use. The same can be applied using StringBuilder: 

StringBuilder sb = new StringBuilder(); 

for (i = 0; i < 1000; i++) 

 sb.Append(i); sb.Append(‘ ‘); 

By using StringBuilder here, you also de-stress the memory allocator. 

 

Do visit our channel to get more information: Click Here

Author:-

Anil Giri

Call the Trainer and Book your free demo Class For C Programming Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*