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.

#1.

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.

#2.

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.

#3.

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.

#4.

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

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

#5.

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.

#6.

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.

#7.

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

#8.

How Hashtable works internally?

#9.

How Hashtable resolves hash collision?

#10.

Dictionary vs Hashtable - which one is faster & why?

#11.

Stack vs Heap Memory?

#12.

Difference between Managed Resources & Unmanaged Resources in .NET?

#13.

How does memory management work in .NET?

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

#14.

How does memory management work for unmanaged resources?

Explain Dispose() vs Finalize()

#15.

Difference between Dispose and Finalize methods?

#16.

Explain IDisposable pattern?

#17.

Difference between String & StringBuilder class?

#18.

Explain how the string is immutable in C#?

#19.

Difference between readonly, const & static keywords?

#20.

Difference between Func, Action & Predicate?