Top 50 Advanced C# Interview Questions & Answers with Examples - Textnotes

Top 50 Advanced C# Interview Questions & Answers with Examples


A complete list of 50 advanced C# interview questions covering threading, performance, memory, delegates, reflection, generics, LINQ, async/await, Span, records, patterns, and .NET internals — each answer includes practical examples.

1. What is SOLID? Explain with C# examples.

Single Responsibility

Open/Closed

Liskov Substitution

Interface Segregation

Dependency Inversion

Example (Single Responsibility):

class InvoicePrinter {
   public void Print() { }
}

class InvoiceCalculator {
   public double Calculate() { return 100; }
}


2. Explain Dependency Injection with example.

Passing dependencies instead of creating them inside a class.

public interface IMessage { void Send(); }

public class EmailMessage : IMessage { public void Send() {} }

public class Notification {
   private readonly IMessage _msg;
   public Notification(IMessage msg) { _msg = msg; }
   public void Notify() => _msg.Send();
}


3. What are Records in C#?

Immutable reference types introduced in C# 9.

public record User(string Name, int Age);


4. What is the difference between record and class?

  1. Record: immutable, value-based equality
  2. Class: mutable reference-based equality


5. What is Span<T>? Why is it used?

A stack-only, fast, memory-safe type for slicing arrays without copying.

Span<int> slice = new int[] {1,2,3,4}.AsSpan(1, 2);


6. What is Memory<T>?

Similar to Span<T> but allowed in async methods.

Memory<byte> mem = new byte[100];


7. What is Reflection? Provide an example.

Inspecting assemblies at runtime.

var props = typeof(Person).GetProperties();


8. What is Emit in C#?

Used to generate IL code at runtime.


9. Explain Covariance & Contravariance.

Covariance: out
Contravariance: in
IEnumerable<string> s = new List<string>();
IEnumerable<object> o = s; // covariance


10. What is Thread-Safety?

Ensuring shared data is safe across threads.

lock(_lockObj) { count++; }


11. What is Deadlock? Example?

Two threads wait forever for each other's locks.


12. What is SemaphoreSlim?

Used to limit concurrent threads.

await semaphore.WaitAsync();


13. What is ReaderWriterLockSlim?

Allows multiple readers but only one writer.


14. Explain async/await state machine.

Compiler breaks async method into states using IAsyncStateMachine.


15. What is ConfigureAwait(false)?

Avoids capturing UI context → better performance.


16. Explain Parallel.ForEach with example.

Used for multi-threaded loops.

Parallel.ForEach(myList, item => DoWork(item));


17. What is CancellationToken?

Used to cancel async tasks.

cts.Cancel();


18. What is immutable class?

Class whose fields cannot be modified.


19. Explain Custom Attributes with example.

[AttributeUsage(AttributeTargets.Class)]
class MyTag : Attribute {}


20. What is Middleware in .NET Core?

Pipeline components that handle HTTP requests.


21. What is Memory Leak in C#?

Occurs when objects are still referenced but unused.


22. What is GC Generation?

3 generations – 0,1,2 for optimized memory cleanup.


23. Explain IDisposable & using pattern.

using(var con = new SqlConnection()) {}


24. What is WeakReference?

Allows object to be collected even if referenced.


25. What is Polymorphism with abstract classes?

abstract class Animal { public abstract void Speak(); }
class Dog : Animal { public override void Speak() {} }


26. What is Method Hiding?

public new void Show() {}


27. What is Expression Tree?

Expression<Func<int>> exp = () => 10;


28. What is IQueryable?

Converts LINQ query to SQL.


29. What is Deferred Execution?

Query executed when enumerated.


30. What is PLINQ?

Parallel LINQ:

var result = list.AsParallel().Where(x => x > 10);


31. What is AutoMapper?

Object-to-object mapping library.


32. What is MediatR?

Mediator pattern library for CQRS.


33. Explain CQRS.

Separates “Read” and “Write” operations.


34. What is Minimal API (.NET 6+)?

app.MapGet("/hello", () => "Hello");


35. What is Dependency Inversion Principle?

High-level modules should not depend on low-level modules.


36. What are Nullable Reference Types?

Avoid null reference exceptions.

string? name = null;


37. What is Pattern Matching?

if(obj is Person { Age: > 18 }) { }


38. What is yield return?

Used for lazy iteration.

yield return item;


39. What is BlockingCollection?

Thread-safe producer-consumer collection.


40. What is ConcurrentDictionary?

Thread-safe dictionary type.


41. What is Mutex?

Used for inter-process locking.


42. What is StopWatch?

High-performance timer.

var sw = Stopwatch.StartNew();


43. What is BenchmarkDotNet?

Library for performance benchmarking.


44. What is AOT Compilation?

Ahead-of-time compilation for performance (.NET 7+).


45. What is Source Generator in C#?

Compile-time code generation.


46. What is IL Code?

Intermediate Language executed by CLR.


47. What is JIT Compiler?

Just-in-Time compiler converting IL to machine code.


48. What is Reflection.Emit?

Generates IL dynamically at runtime.


49. What is Dynamic Keyword?

dynamic value = "Hi";
value = 100;


50. What is Repository Pattern?

public interface IUserRepo {
   User GetUser(int id);
}