C# Interview Questions and Answers
C# language and CLR interview questions — value vs reference types, generics, delegates, LINQ, async/await, memory and the type system.
- 01 What are generics in C#? Generics let you write a class, method, interface or delegate with a placeholder type parameter (T) that the caller…
- 02 What is the difference between virtual, override and new (method hiding)? virtual marks a base method as overridable. override in a derived class replaces it — calls are dispatched to the…
- 03 What is the difference between method overloading and method overriding? Overloading = multiple methods with the same name but different parameters in the same type; the compiler picks one…
- 04 What are the four pillars of object-oriented programming? Encapsulation — bundle data with the methods that operate on it and hide the internal state behind a controlled…
- 05 What is the difference between composition and inheritance, and why prefer composition? Inheritance models “is-a” — a subclass gets the base class's members automatically. Composition models “has-a” — a…
- 06 What is the difference between const and readonly in C#? const is a compile-time constant — its value is baked into the calling code at compile time, must be set at…
- 07 What is a record in C# and how is it different from a class? A record is a reference type (or record struct for a value type) designed for immutable data. The compiler generates…
- 08 What is pattern matching in C#? Pattern matching tests a value against a shape and (optionally) extracts data from it in one expression. It covers…
- 09 What are nullable reference types in C#? Nullable reference types (opt-in since C# 8, on by default in new projects) make the compiler track whether a…
- 10 What is IDisposable and the Dispose pattern? IDisposable defines Dispose(), a place to release resources the garbage collector doesn't manage — file handles, DB…
- 11 What is covariance and contravariance in C#? Covariance (out) lets you use a more-derived type than specified — IEnumerable<string> is assignable to…
- 12 What does the sealed keyword do in C#? sealed on a class prevents any other class from inheriting it. sealed on an override method stops further derived…
- 13 What is the difference between the is and as operators? is tests whether an object is compatible with a type and returns bool (and can declare a variable: if (o is Customer…
- 14 What is an extension method in C#? An extension method is a static method in a static class whose first parameter is marked this SomeType x. It can then…
- 15 What is the difference between a Task and a Thread? A Thread is an actual OS thread — expensive to create (~1 MB stack), you manage its lifetime. A Task is a higher-level…
- 16 What is a lambda expression, and what is a closure? A lambda is an inline anonymous function: x => x * 2. It becomes a delegate (Func/Action) or, for LINQ providers, an…
- 17 How does exception handling work in C#, and what are the best practices? try runs code that might fail, catch handles specific exception types, finally always runs (cleanup). Best practices:…
- 18 What is the difference between an array and a List<T> in C#? An array is a fixed-size, contiguous block of memory — fast, low overhead, but you can't add or remove elements. A…
- 19 What is the difference between IEnumerable, ICollection and IList? They form a hierarchy of capability. IEnumerable<T> — iterate only (foreach). ICollection<T> — adds Count, Add,…
- 20 When should you use a struct instead of a class? Use a struct only when the type is small (roughly ≤ 16–24 bytes), logically a single value, immutable, and frequently…
- 21 What is the difference between an empty string, null, and IsNullOrWhiteSpace? null means “no string reference” — calling a method on it throws. "" (or string.Empty) is a real, zero-length string.…
- 22 What is the difference between `throw;` and `throw ex;`? throw; re-throws the current exception preserving its original stack trace and throw point. throw ex; re-throws it but…
- 23 What does the yield keyword do in C#? yield return turns a method into an iterator that produces a sequence lazily — each call to MoveNext() runs the method…
- 24 What is the difference between First, FirstOrDefault, Single and SingleOrDefault? First — first match, throws if none. FirstOrDefault — first match or default(T) if none. Single — exactly one match,…
- 25 What is deferred vs immediate execution in LINQ? Deferred operators (Where, Select, OrderBy, Take) build a pipeline and don't run until you enumerate. Immediate…
- 26 How does equality work in C# (==, Equals, ReferenceEquals, GetHashCode)? For reference types, == and Equals default to reference equality (same object). Override Equals and GetHashCode…
- 27 What is the difference between an abstract method and a virtual method? An abstract method has no body and must be overridden by a non-abstract derived class — it only exists in an abstract…
- 28 What is the difference between a shallow copy and a deep copy? A shallow copy duplicates the top-level object but its reference-type fields still point at the same nested objects —…
- 29 Why should you avoid `async void`? async void can't be awaited, so callers can't know when it finishes or catch its exceptions — an unhandled exception…
- 30 What does ConfigureAwait(false) do? By default, after an await, execution resumes on the captured SynchronizationContext (the UI thread, or classic…
- 31 What is a CancellationToken and how do you use it? A CancellationToken is a cooperative cancellation signal. A CancellationTokenSource creates and controls it; you pass…
- 32 What is the difference between Tuple and ValueTuple in C#? Tuple (System.Tuple) is a reference type with Item1, Item2… — heap-allocated, immutable, no names. ValueTuple (the…
- 33 What does the params keyword do? params lets a method accept a variable number of arguments as an array: void Log(params string[] parts) can be called…
- 34 What is the difference between a static class and a singleton? A static class can't be instantiated, has only static members, and can't implement an interface or be injected — it's…
- 35 What are the access modifiers in C#? public — anywhere. private — only the containing type (default for class members). protected — the type and its…
- 36 If I pass an object to a method and change its properties, does the caller see the change? Yes for mutating the object's members (both variables point to the same instance). No for reassigning the parameter to…
- 37 What is IAsyncEnumerable and when would you use it? IAsyncEnumerable<T> is a sequence you produce and consume asynchronously — await foreach (var x in source). Each item…
- 38 How does the C# compiler decide which overload to call? Overload resolution picks the “best” applicable method: an exact type match beats an implicit conversion; a more…
- 39 What is the difference between a finalizer and Dispose? Dispose() is deterministic cleanup you call (via using) to release resources now. A finalizer (~ClassName()) is a…
- 40 What is the difference between Dictionary and ConcurrentDictionary? Dictionary<K,V> is not thread-safe — concurrent writes (or a write during a read) corrupt it or throw.…
- 41 What is the difference between Parallel.ForEach and awaiting a set of tasks? Parallel.ForEach is for CPU-bound work — it spreads iterations across cores and blocks the calling thread until done.…
- 42 What is explicit interface implementation, and why use it? Explicit implementation — void IDisposable.Dispose() { } — makes the member accessible only through the interface, not…
- 43 What is an enum in C#, and what are its pitfalls? An enum is a value type with a set of named integer constants — enum Status { Active, Suspended, Closed }. Pitfalls:…
- 44 What are checked and unchecked contexts in C#? By default, integer arithmetic that overflows wraps silently (int.MaxValue + 1 becomes int.MinValue). A checked…
- 45 What is the difference between a field and a property in C#? A field is a variable that directly holds data. A property is a pair of accessor methods (get/set) that look like a…
- 46 What are ref returns and ref locals in C#? A ref return returns a reference to a variable (e.g. an array slot) rather than a copy, so the caller can read and…
- 47 What is the difference between a namespace and an assembly? A namespace is a logical grouping of type names to avoid clashes and organise code — purely a naming device, resolved…
- 48 Why is calling .Result or .Wait() on a Task dangerous? Blocking on an async operation with .Result or .Wait() ties up a thread doing nothing, and in a context with a…
- 49 What is the difference between a jagged array and a multidimensional array? A multidimensional array (int[,]) is a single rectangular block — every row has the same length. A jagged array…
- 50 What is the difference between an implicit and an explicit conversion operator? An implicit conversion happens automatically with no cast — define public static implicit operator when the conversion…
- 51 What is a WeakReference, and when would you use one? A normal reference keeps an object alive. A WeakReference (or WeakReference<T>) lets the GC collect the object if…
- 52 What is a static constructor, and when does it run? A static constructor (static MyClass() { }) initialises static fields and runs once per type, automatically, before…
- 53 When should you create a custom exception type? Create a custom exception when callers need to catch that specific failure and handle it differently — a…
- 54 What is the difference between a delegate and an event? An event is a delegate field with restricted access: outside code can only += / -= to it — it can't invoke it or…
- 55 What generic constraints does C# support? where T : — class / struct (reference/value type), new() (has a public parameterless constructor), a base class name,…
- 56 What is the difference between StringComparison options, and why do they matter? Ordinal compares by raw char values — fast, deterministic, right for identifiers, keys, file paths, protocol tokens.…
- 57 What are primary constructors in C# 12? Primary constructors (C# 12) let you declare constructor parameters right on the class/struct declaration — class…
- 58 When would you use a LinkedList instead of a List? Almost never in practice. A LinkedList<T> gives O(1) insert/remove if you already hold the node, but it has no index…
- 59 What do ??, ??= and ?. do in C#? a ?? b — return a unless it's null, then b. a ??= b — assign b to a only if a is currently null. a?.b — access b only…
- 60 What is C#? C# is a modern, statically typed, object-oriented (and increasingly functional) programming language from Microsoft.…
- 61 What is the difference between a value type and a reference type? A value type (struct, enum, int, bool, DateTime) holds its data directly; assigning or passing it copies the value. A…
- 62 What is boxing and unboxing in C#? Boxing is wrapping a value type in an object (or interface) reference so it can live on the heap — it happens…
- 63 What is the difference between IEnumerable and IQueryable? IEnumerable<T> represents an in-memory sequence; LINQ operators run as compiled delegates and iterate objects…
- 64 What is the difference between an abstract class and an interface? An abstract class is a base class you can't instantiate; it can hold state (fields), constructors, and a mix of…
- 65 What are delegates in C#? A delegate is a type-safe reference to a method — effectively a function pointer with a signature. You can store…
- 66 What is LINQ in C#? LINQ (Language-Integrated Query) is a uniform way to query data — in-memory collections, databases, XML, JSON — using…
- 67 How do async and await work in C#? async/await let you write non-blocking asynchronous code that reads sequentially. await on an incomplete Task returns…
- 68 What is the difference between ref and out parameters? Both pass an argument by reference so the method can modify the caller's variable. ref requires the variable to be…
- 69 What is the difference between string and StringBuilder? string is immutable — every concatenation or modification allocates a new string object. StringBuilder is a mutable…
- 70 What is a tuple in C#, and when should you use one? A tuple bundles a few values into one lightweight object without declaring a type: (string name, int age) p = ("Sam",…
- 71 Why must you override GetHashCode whenever you override Equals? The contract: if two objects are Equals, they must return the same GetHashCode. Hash-based collections (Dictionary,…
- 72 What is immutability, and why is it valuable? An immutable object can't change after construction. Benefits: it's inherently thread-safe (no locking on shared…
- 73 What is database connection pooling, and why does it matter? Opening a physical database connection (TCP handshake, auth, TLS) is expensive. ADO.NET keeps a pool of open…
- 74 What is a CancellationToken, and how do you use it properly? A CancellationToken is a cooperative signal that an operation should stop. Callers create a CancellationTokenSource,…
- 75 Can you have a memory leak in .NET despite garbage collection? Yes. The GC only reclaims objects that are unreachable. If something keeps a reference alive forever, that memory is…
- 76 What is the Large Object Heap, and why is it a concern? Objects of 85,000 bytes or more (mostly big arrays and strings) go on the Large Object Heap. The LOH is collected only…
- 77 What is the difference between a process and a thread? A process is an isolated running program with its own virtual address space; the OS protects one process's memory from…
- 78 What is a local function in C#, and how is it different from a lambda? A local function is a named method declared inside another method, visible only there — useful for a small helper…
- 79 What do the checked and unchecked keywords do in C#? By default, integer arithmetic in C# silently wraps around on overflow (int.MaxValue + 1 becomes a large negative…
- 80 What is a partial class, and when is it used? The partial keyword lets a single class (or struct/interface) be split across multiple files; the compiler merges them…
- 81 What is reflection in C#, and what are its trade-offs? Reflection lets code inspect and invoke types, members and attributes at runtime that weren't known at compile time —…
- 82 What is operator overloading in C#, and when is it appropriate? C# lets a type define what an operator means for it, via public static T operator +(T a, T b). It's appropriate for…
- 83 What is an inner exception, and how do you use it? The inner exception is the original exception preserved inside a new one you throw to add context: throw new…
- 84 How does string interpolation work, and is it slower than concatenation? $"Hello {name}" is compiled to a string.Format / string.Concat call, or since C# 10 to an efficient interpolated…
- 85 What is the difference between an interface and an abstract class for a plugin contract? Use an interface for the pure contract that plugins must satisfy — multiple implementations, no shared code, easy to…
- 86 What is aspect-oriented programming, and how do you do it in .NET? AOP factors out cross-cutting concerns — logging, caching, retry, transactions, validation — that would otherwise be…
No questions match those filters
Try removing a filter or .
About these C# questions
C# questions are the backbone of almost every .NET interview. Interviewers use them to check that you understand what the code you write actually does at runtime — how the CLR lays out memory, when the garbage collector runs, why struct and class behave differently, and how features like generics, delegates, iterators and async/await are implemented rather than just used.
The questions here are ordered from language fundamentals toward runtime and concurrency topics. For 3–5 year roles, expect deeper follow-ups on the difference between IEnumerable<T> and IQueryable<T>, closure capture, Task scheduling and how using/IDisposable interacts with the GC.