How to Pass a C# Interview: Top Advanced Questions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Pass a C# Interview: Top Advanced Questions



An interview for a Senior C# developer position is not just a test of syntax, but also a deep understanding of the internal workings of .NET, performance optimization, and architectural decisions. In this article, we will break down key topics that often come up in technical interviews. You will learn how to answer complex questions and get ready-made code examples to demonstrate your skills.



1. Asynchronous Programming: async/await and Task



Questions about async and await are a classic in any advanced interview. Interviewers want to see that you understand how the synchronization context works and how to avoid deadlocks.



Typical question: "What happens if you call .Result or .Wait() on an async method in a UI thread?"



Answer: A deadlock will occur because the main thread is blocked waiting for the result, while the continuation of the async method tries to return to the same synchronization context, which is occupied. The solution is to use ConfigureAwait(false) or make the call fully asynchronous.



public async Task<string> GetDataAsync(){    // Correct work with context    var result = await SomeHttpCallAsync().ConfigureAwait(false);    return result;}

// Error: deadlock in UI/ASP.NET Core (before .NET Core 2.1)// var data = GetDataAsync().Result;


Additional question: "What is the difference between Task.Run and Task.Factory.StartNew?"



Task.Run uses the default thread pool and automatically unwraps nested tasks. StartNew gives more control (for example, you can specify TaskCreationOptions.LongRunning), but requires manual Unwrap() management for async delegates.



2. LINQ and Deferred Execution



LINQ is a powerful tool, but its incorrect use can lead to performance degradation. Advanced questions often concern IQueryable vs IEnumerable and query materialization.



Question: "Why does the following code make multiple database queries?"



var query = context.Products.Where(p => p.Price > 100);foreach (var product in query){    Console.WriteLine(product.Name);}var count = query.Count(); // Second query!


Answer: IQueryable represents an expression (Expression Tree) that is translated into SQL. Every time you iterate or call an aggregate function (Count, Sum), the SQL is sent again. To avoid this, you need to materialize the data: var list = query.ToList();



Advanced nuance: The difference between First() and FirstOrDefault() — First() throws an exception if the sequence is empty, which can be more expensive in terms of error handling.



3. Memory Management and Garbage Collector (GC)



Understanding how the GC works is a sign of a Senior developer. Questions may concern generations, finalization, and the Dispose pattern.



Question: "What is the Large Object Heap (LOH) and why should you be careful with it?"



Answer: LOH is the heap for objects larger than 85,000 bytes. Unlike the Small Object Heap, LOH is not compacted (before .NET 4.5.1), which leads to memory fragmentation. This can cause an OutOfMemoryException even if there is plenty of free memory. Recommendations: use object pools or split large arrays into parts.



// Example of bad practicebyte[] largeBuffer = new byte[100_000]; // Goes to LOH

// Better: use ArrayPoolbyte[]

Blogs

Book Recommendations