C# Interview Cheat Sheet
Top 50 interview questions with concise answers. Print this page or save as PDF for offline study.
1. What are value types and reference types?
Value types (int, bool, struct) store their actual data on the stack — copy one and you get two independent values. Reference types (class, string, array) store a pointer on the stack to heap data — copy a reference and both variables point to the same object. This distinction matters a lot when passing objects to methods.
2. What is boxing and unboxing?
3. What is garbage collection?
GC is .NET's automatic memory cleaner. Instead of freeing memory manually, the GC scans the heap, finds objects with no references, and reclaims that memory. It uses generations (Gen0, Gen1, Gen2) — young objects get collected more often since most objects are short-lived.
4. What is an interface?
An interface is a contract — any class that implements it must have these methods and properties. No implementation, just signatures. This lets you write code against the interface rather than a specific class, making things easy to swap, test, and extend.
5. What is abstraction?
Abstraction means hiding complex internals and exposing only what's needed. When you call List.Add(), you don't care how it resizes internally — you just use it. In C#, achieved through interfaces and abstract classes. Keeps code focused on what rather than how.
6. What is encapsulation?
Encapsulation means bundling your data and the methods that work on it inside a class, and controlling who can access what. Fields stay private; you expose them through public properties. Prevents outside code from directly tampering with your object's internal state.
7. What are access modifiers in C#?
Access modifiers control visibility. public — anyone can access. private — only that class. protected — the class and its subclasses. internal — only within the same assembly. protected internal — subclasses or same assembly. Default for class members is private.
8. What is polymorphism?
Polymorphism means one interface, many behaviors. Call animal.Speak() on a Dog, Cat, or Bird — each responds differently through the same method signature. Achieved via method overriding (runtime) and method overloading (compile-time). One of OOP's most powerful features.
9. What is method overriding?
When a child class provides its own version of a parent class method, that's overriding. The parent marks it virtual, the child uses override. At runtime, even holding a base class reference, the child's version gets called — that's the whole point.
10. What is a constructor?
A constructor is a special method that runs automatically when you create an object with new. It sets up initial state. A class can have multiple constructors with different parameters. If you write none, C# provides a default parameterless one automatically.
11. What is a static constructor?
A static constructor runs once — the very first time the class is used — and initializes static members. You cannot call it manually or pass parameters. Perfect for one-time setup like loading config or initializing shared resources.
12. What is an exception?
An exception is a runtime error — null reference, divide by zero, file not found, etc. Handle with try-catch-finally. Catch the specific exception type you expect, not the base Exception blindly. Always clean up resources in the finally block.
13. What is the difference between == and Equals()?
For value types, both compare values. For reference types, == by default checks if they point to the same object in memory. Equals() can be overridden to compare content. Strings override both to compare content — that's why "hello" == "hello" is true even for different string objects.
14. What is C#?
C# is Microsoft's general-purpose, object-oriented language that runs on .NET. Think of it as Java's cousin — strongly typed, memory-safe, and packed with modern features like async/await, LINQ, and generics. Used for everything from web APIs to game dev (Unity) to desktop apps.
15. What is the .NET Framework / .NET Runtime?
.NET is the runtime that runs your C# code. It handles memory management, compiles your IL to machine instructions (JIT), and provides a massive standard library. .NET Core/.NET 5+ is the modern cross-platform version; .NET Framework is the older Windows-only one.
16. What is a namespace?
A namespace is basically a folder for your code. It groups related classes together and prevents naming conflicts — so MyApp.Models.User and ThirdParty.Models.User never clash. You import them using the 'using' keyword at the top of a file.
17. What is a nullable type in C#?
By default, value types like int cannot be null. Nullable types (int? or Nullable) allow them to hold null — useful for optional values like database columns. Check with .HasValue, read with .Value, or use ?? for a default fallback.
18. What is the difference between Array and List?
Arrays are fixed-size — defined upfront, never changes. Lists are dynamic — grow and shrink as needed. Arrays are slightly faster for indexed access. Lists give you Add(), Remove(), Contains() and more. Use List by default unless size is fixed and performance is critical.
19. What is the purpose of the var keyword?
var lets the compiler infer the type at compile time — it is not dynamic. var x = 42 is identical to int x = 42 at runtime. Use it when the type is obvious from context, like LINQ results or long generic types. Avoid it when it makes code harder to read.
20. What is an enum?
An enum is a named set of integer constants. Instead of magic numbers like 1=Active, 2=Inactive, you write Status.Active and Status.Inactive. Makes code readable and less error-prone. Under the hood, each value maps to an int starting at 0 by default.
21. What are properties in C#?
Properties are the clean way to expose class data. Instead of making a field public (risky), you write get and set accessors with validation or logic inside. Auto-properties (public string Name { get; set; }) handle the simple case. Init-only setters allow setting only during object construction.
22. What is an indexer?
An indexer lets you access an object using square bracket syntax — just like arrays. Defined with this[int index]. A custom Matrix class could let you do matrix[0, 1] instead of exposing internal arrays. Syntactic sugar that makes your class feel natural to use.
23. What is a delegate?
A delegate is a type-safe function pointer — it holds a reference to a method and lets you call it indirectly. You can pass methods as parameters or store them in variables. Action, Func, and Predicate are the built-in generic delegates you will use most of the time.
24. What is an event in C#?
An event is a delegate with restrictions — only the declaring class can invoke it, but anyone can subscribe. Standard pattern for notifications: button clicks, data changes, etc. Events prevent subscribers from replacing the delegate chain or invoking each other directly.
25. What is the difference between break and continue?
Both work inside loops. break exits the loop entirely — done, move on. continue skips the rest of the current iteration and jumps to the next one. Think of break as stop the loop and continue as skip this round but keep going.
26. What is the difference between ref and out keywords?
Both pass by reference but with different rules. ref requires the variable to be initialized before passing — the method can read and modify it. out does not require pre-initialization, but the method must assign a value before returning. Use out for returning multiple values from a method.
27. What is a sealed class?
A sealed class cannot be inherited — no subclasses allowed. Use it to lock down your design and prevent misuse. The string class in .NET is sealed. It also lets the compiler optimize virtual method calls since no overrides are possible.
28. What is a partial class?
A partial class splits one class definition across multiple files — the compiler merges them. Used heavily with auto-generated code like Visual Studio designer files so your handwritten logic stays separate from the generated portion.
29. What is the purpose of the using statement?
The using statement guarantees Dispose() is called when the block ends — even if an exception is thrown. Use with anything holding unmanaged resources: file streams, DB connections, HTTP clients. Without it, you must manually call Dispose() in a finally block every time.
30. What is a thread in C#?
A thread is the smallest unit of execution — a path of code running through your program. By default your app runs on one thread. Additional threads run work in parallel. But threads are expensive and share memory, so misuse causes race conditions and unpredictable bugs.
31. What is multithreading?
Multithreading lets your app do multiple things simultaneously on separate threads. Great for CPU-heavy work or keeping UIs responsive. Since threads share memory, you need synchronization (locks, semaphores) to avoid data corruption when multiple threads access the same data.
32. What is the lock keyword used for?
The lock keyword prevents two threads from entering the same block of code at the same time. One thread acquires the lock; others wait. Always lock on a private dedicated object — not this — to avoid accidental interference from outside code.
33. What is Just-In-Time (JIT) compilation?
JIT (Just-In-Time) compilation converts .NET IL code into machine code at runtime, right before it is needed. The first call triggers compilation; subsequent calls use cached machine code. This gives cross-platform portability while still running at near-native speed.
34. What is the difference between is and as operators?
is checks if an object is compatible with a type — returns true or false, never throws. as attempts a cast and returns null on failure instead of throwing InvalidCastException. Modern C# combines both: if (obj is MyType x) checks the type and assigns in one clean step.
35. What is the base keyword?
The base keyword lets a derived class access its parent's members. Most commonly used to call the parent constructor via base(param), or invoke the parent version of an overridden method via base.MethodName(). Without it, there is no way to reach the parent's overridden implementation.
36. What is the purpose of the readonly struct?
A readonly struct guarantees all fields are immutable — the compiler enforces it. This lets the runtime safely pass it by reference internally without defensive copies. Use it for small, value-like objects such as coordinates, colors, or time ranges.
37. What is the difference between a field and a property?
A field is a raw variable inside a class — just data storage, no logic. A property wraps it with get and set accessors where you add validation, lazy loading, or computed values. Properties are the public API for your data; fields should always stay private.
38. What is an anonymous type in C#?
An anonymous type lets you create a throwaway object without defining a class: var p = new { Name = "John", Age = 25 }. The compiler generates the class with read-only properties behind the scenes. Used mostly in LINQ projections where you need a temporary data shape.
39. What is the difference between IEnumerable and IEnumerator?
IEnumerable is the collection — it exposes GetEnumerator() which returns an IEnumerator. IEnumerator is the cursor that walks through items — it has MoveNext(), Current, and Reset(). You implement IEnumerable on your class so it works with foreach. You rarely deal with IEnumerator directly.
40. What is the difference between IEnumerable and IQueryable?
Both let you query collections but where the query runs differs. IEnumerable pulls all data to memory first, then filters in C#. IQueryable builds an expression tree and sends it to the data source (like SQL Server) — filtering happens there. For large datasets, always prefer IQueryable.
41. What is the purpose of extension methods?
Extension methods let you add new methods to existing types without touching their source code or subclassing. Write a static method in a static class with this TypeName as the first parameter. They appear as instance methods on that type. All of LINQ is built with extension methods.
42. What is a static class?
A static class cannot be instantiated — you can never do new MathHelper(). All members must also be static. Use it for utility functions that need no state — math operations, string formatting, or extension method containers. Static classes are sealed by default.
43. What is the base class for all types in C#?
Everything in C# ultimately inherits from System.Object (aliased as object). This gives every type — even int — methods like ToString(), Equals(), GetHashCode(), and GetType(). That is why you can assign anything to an object variable, though boxing happens for value types.
44. What is a destructor in C#?
A destructor runs when the GC decides to clean up the object — you cannot call it yourself and cannot control when it runs. Avoid using destructors for resource cleanup; use IDisposable and using instead. Destructors delay garbage collection and add measurable overhead.
45. What is method hiding in C#?
Method hiding occurs when a derived class defines a method with the same name as the base using the new keyword instead of override. Unlike overriding, if you hold a base class reference, the base version gets called — the derived version only runs through a derived reference.
46. What is the difference between early binding and late binding?
Early binding resolves method calls at compile time — the compiler knows exactly which method to call, fast and type-safe. Late binding resolves at runtime — used with dynamic, reflection, or virtual dispatch. Late binding is flexible but slower and can fail at runtime.
47. What is the yield keyword used for?
yield creates a lazy iterator — it returns values one at a time without building a full collection upfront. Execution pauses at yield return and resumes where it left off on the next call. Great for large sequences where you do not want everything loaded into memory at once.
48. What is a generic type?
Generics let you write type-safe code that works with any data type. List works for List, List, or any type — no boxing, no casting, compile-time error checking. Before generics, object-based collections were the only option — error-prone and slow.
49. What is covariance and contravariance?
These control how generic types relate when type parameters have an inheritance relationship. Covariance (out T) lets you use IEnumerable where IEnumerable is expected — safe because you only read. Contravariance (in T) allows Action where Action is expected — safe because you only write.
50. What is dependency injection in C#?
DI means giving a class its dependencies from outside rather than letting it create them itself. Instead of new SqlRepository() inside a service, you receive an IRepository through the constructor. Makes classes easy to test (swap real for mock) and keeps them loosely coupled. ASP.NET Core has DI built in.