C# · Practice

Interview Questions

28 questions you are likely to be asked about C#, from beginner to experienced level — each with a spoken-style answer, a common follow-up and a link to the lesson behind it.

Advanced 15 min Topic 26 of 26
0/26 in C#

Beginner questions (11)

01What is the difference between value types and reference types?

Value types (int, struct, record struct) hold their data directly and are copied on assignment. Reference types (class, string, arrays) hold a reference to an object on the heap; assignment copies the reference.

Learn: Variables and Data Types
02What is the difference between const and readonly?

const is a compile-time constant, implicitly static, and must be a primitive or string. readonly is set at runtime — in the declaration or constructor — and can hold any type.

Learn: Variables and Data Types
03What are nullable reference types?

A C# 8+ feature where reference types are non-nullable by default and string? marks values that may be null. The compiler warns when you might dereference null.

Learn: Nullable Types
04String vs StringBuilder?

Strings are immutable: every concatenation creates a new string. StringBuilder modifies an internal buffer, which is much faster when building strings in loops.

Learn: Strings
05What is the difference between C#, .NET and the CLR?

C# is the language. .NET is the platform: runtime, base class libraries, SDK and tooling. The CLR (Common Language Runtime) loads the compiled IL, JIT-compiles it to machine code and manages memory with the garbage collector.

Learn: Introduction to C#
06What are top-level statements?

Since C# 9 the entry point can be plain statements in Program.cs without a Main method or class. The compiler generates Main for you. Only one file per project can use them.

Learn: Installing the .NET SDK
07What does var mean in C#?

Implicit typing: the compiler infers the static type from the right-hand side, and it cannot change later. It is not dynamic. Use it when the type is obvious from the expression.

Learn: Variables and Data Types
08What is the difference between == and Equals for strings and objects?

For strings both compare content. For classes, == compares references unless the operator is overloaded, while Equals can be overridden for value equality. Records generate value equality for both.

Learn: Operators
09for vs foreach vs while?

for suits index-based loops, foreach iterates any IEnumerable without an index and cannot modify the collection, while repeats until a condition becomes false, and do…while runs at least once.

Learn: Loops
10What are ref, out and in parameters?

ref passes a variable by reference and it must be initialised first. out must be assigned by the method — used by TryParse. in passes by reference read-only, mainly to avoid copying large structs.

C# Illustrative
if (int.TryParse("42", out var number))
    Console.WriteLine(number + 1); // 43
Learn: Methods
11Array vs List<T> vs Dictionary<TKey, TValue>?

An array has a fixed length. List<T> grows dynamically and is the default collection. Dictionary gives fast lookup by key. HashSet<T> stores unique values with fast Contains.

Learn: Arrays and Collections

Intermediate questions (10)

01What is deferred execution in LINQ?

Most LINQ operators build a query that runs only when it is enumerated. Enumerating it twice runs it twice, which with EF Core means two database queries.

Learn: LINQ
02What are delegates and events?

A delegate is a type-safe reference to a method (Func<>, Action<>). An event is a restricted delegate that outside code can only subscribe to or unsubscribe from — only the owning class can raise it.

Learn: Delegates and Events
03What are records?

Types designed for immutable data with value-based equality, concise positional syntax, built-in ToString and non-destructive mutation with the with expression.

C# Illustrative
public record Point(int X, int Y);
var a = new Point(1, 2);
var b = a with { Y = 5 };
Console.WriteLine(a == new Point(1, 2)); // True
Learn: Records
04IEnumerable vs IQueryable?

IEnumerable executes LINQ in memory. IQueryable builds an expression tree that a provider such as EF Core translates into SQL, so filtering happens in the database.

Learn: LINQ
05How does exception handling work, and what does finally do?

try contains risky code, catch handles specific exception types (most specific first), and finally always runs for cleanup. Use throw; to rethrow and preserve the stack trace, and using for IDisposable resources.

Learn: Exception Handling
06What are generics and why are they better than object?

Generics (List<T>, Repository<T>) let you write type-safe reusable code. Compared with object they avoid casts, catch errors at compile time and avoid boxing for value types.

Learn: Generics
07What is a lambda expression?

An anonymous function written with =>: x => x * 2. Lambdas are converted to delegates (Func, Action, Predicate) or expression trees, which is how EF Core translates LINQ to SQL.

Learn: Lambda Expressions
08What is an interface in C# and what can it contain?

A contract of members a class must implement. It can contain methods, properties, events and indexers, plus default implementations and static members since C# 8/11, but no instance fields.

Learn: Interfaces
09What is boxing and unboxing?

Boxing wraps a value type in an object on the heap (object o = 42); unboxing casts it back. It allocates and costs performance, so generics are preferred over object-based collections like ArrayList.

Learn: Variables and Data Types
10How do you write unit tests in C#?

With xUnit: [Fact] for one case and [Theory] with [InlineData] for many. Arrange–Act–Assert, test one behaviour per test, and use Moq or NSubstitute to fake interfaces.

Learn: Unit Testing with xUnit

Experienced questions (7)

01Why should you avoid .Result and .Wait() on tasks?

They block a thread while waiting. In ASP.NET Core this wastes thread-pool threads and reduces throughput; in UI apps or with certain synchronisation contexts it can deadlock. Use await all the way.

Learn: Async and Await
02What is pattern matching in modern C#?

Concise syntax for testing a value’s shape: type patterns, property patterns, relational patterns, list patterns and switch expressions.

C# Illustrative
string Describe(object o) => o switch
{
    int n when n < 0 => "negative",
    int => "number",
    string { Length: 0 } => "empty string",
    null => "null",
    _ => "other",
};
Learn: Pattern Matching
03Task vs ValueTask, and what does async void risk?

Task is the default for async methods. ValueTask avoids an allocation when results are often available synchronously but has usage restrictions. async void cannot be awaited and its exceptions crash the process — use it only for event handlers.

Learn: Async and Await
04How does garbage collection work in .NET?

Objects are allocated on a managed heap. The GC periodically finds unreachable objects and reclaims memory, using generations (0, 1, 2) because most objects die young. Large objects go on the large object heap. Unmanaged resources still need IDisposable.

Learn: Classes and Objects
05What is IDisposable and the dispose pattern?

IDisposable.Dispose releases unmanaged resources (files, sockets, database connections) deterministically. using var and await using call it automatically. Finalizers are only a safety net for unmanaged handles.

Learn: Exception Handling
06How do you run several async operations in parallel?

Start the tasks without awaiting, then await Task.WhenAll(tasks). For a bounded number of parallel operations use Parallel.ForEachAsync or SemaphoreSlim. Pass CancellationToken through every async call.

Learn: Async and Await
07What are extension methods?

Static methods in a static class whose first parameter has this, so they can be called like instance methods: text.Truncate(20). LINQ is built from them. They cannot access private members.

Learn: LINQ
Progress is saved in this browser. Sign in to sync.