Top 74 C# Interview Questions

C# is an object-oriented programming language developed by Microsoft. C# code gets compiled into MSIL that further executes on CLR.


What are types in C#?

C# is a strongly-typed language. It means any variable that we define must have a type such as integer, float, decimal, text, etc.

C# mainly categorized data types in two types:

  • Value types
  • Reference types

Value types include simple types (such as int, float, bool, and char), enum types, struct types, and Nullable value types.

Reference types include class types, interface types, delegate types, and array types.


Difference between Value types & Reference types in C#?

In C#, these data types are categorized based on how they store their value in the memory.

Value Type

A data type is a value type if it holds a data value within its own memory space. It means the variables of these data types directly contain values.

  • Fixed memory size
  • Stored in Stack memory
  • Holds actual value
  • i.e. int, char, bool, etc.

Reference types

Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored.

  • Not fixed memory
  • Stored in Heap memory
  • Holds memory address of actual value
  • i.e. string, array, class, etc.

Good to know

  • Value types are stored wherever they are declared. For example, an int's value inside a function as a local variable would be stored on the stack, whilst an int's value declared as member in a class would be stored on the heap with the class it is declared in.
  • When a reference type variable is declared, it contains the value null until an object that has been created using the keywords new is assigned to it.


What is the use of a finally block in exception handling?

The finally block will execute when the try/catch block leaves the execution, no matter what condition cause it. It always executes whether the try block terminates normally or terminates due to an exception. The main purpose of finally block is to release the system resources.

Conditions of leaving try/catch block could be anything including execution of a break, continue, goto, or return statement, or propagation of an exception out of the try statement.


Does the finally block execute if the code throws an error?

Yes, a finally block always executes, regardless of whether an exception is thrown.


Does the finally block execute if the code returns from the try block without any exception?

Yes, the finally block is executed however the flow leaves the try block - whether by reaching the end, returning, or throwing an exception.


What is the difference between throw and throw ex?

throw preserves the stack trace while throw ex does not.

If you use throw ex, original stack trace will be wiped out completely and you will be left with only the stack trace from where you threw the exception. Hence It's recommended to use throw instead of throw ex.


What is the difference between Class & Struct in C#?


How Hashtable works internally?


How Hashtable resolves hash collision?


Dictionary vs Hashtable - which one is faster & why?


Stack vs Heap Memory?


Difference between Managed Resources & Unmanaged Resources in .NET?


How does memory management work in .NET?

Explain Gen0, Gen1, Gen2 here, also Stack vs Heap.


How does memory management work for unmanaged resources?

Explain Dispose() vs Finalize()


Difference between Dispose and Finalize methods?


Explain IDisposable pattern?


Difference between String & StringBuilder class?


Explain how the string is immutable in C#?


Difference between readonly, const & static keywords?


Difference between Func, Action & Predicate?