📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

πŸ“š .NET Interview Questions

377 questions for senior .NET engineer interviews, each with a 30-second answer, the full answer, a deep dive, the follow-ups interviewers ask next, and real-world production problems.

  1. How do lambda expressions work in C#? Explain expression versus statement lambdas, natural types, static lambdas, attributes, explicit return types, default parameters (C# 12), parameter modifiers without types (C# 14), and how lambdas compile into closures.

    Lambdas in C# 14: syntax forms, conversion to delegates or expression trees, natural type inference, modern features, closure compilation and allocation cost.

    Mid-Level Theory Delegates and Events Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  2. How does LINQ to Objects work under the hood? Explain how operators like Where and Select are implemented as extension methods and iterators, query syntax versus method syntax, streaming versus buffering operators, and the newer operators added in .NET 6 to .NET 10.

    LINQ to Objects internals: extension methods over IEnumerable, iterator pipelines, operator fusion and fast paths, query syntax translation, streaming versus buffering, and modern operators such as Chunk, CountBy, Index and LeftJoin.

    Mid-Level Theory LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  3. What was LINQ to SQL, and which of its concepts still matter for a .NET 10 developer using EF Core? Explain the provider model, DataContext and change tracking, identity map, deferred loading, parameterised SQL generation and why LINQ to SQL is not available on modern .NET.

    LINQ to SQL as the original IQueryable provider: its architecture and lasting concepts (translation, change tracking, identity map, lazy loading, parameterisation) and how they map to EF Core today.

    Mid-Level Theory LINQ Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  4. How does LINQ to Entities work in EF Core 10? Explain how a LINQ query is translated to SQL, what can and cannot be translated, client evaluation rules, tracking versus no-tracking, split queries, compiled queries, and how you inspect and tune the generated SQL.

    LINQ to Entities with EF Core: the query pipeline, translation limits and client evaluation, tracking, projections, split queries, compiled queries and SQL inspection tools.

    Senior Theory LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  5. A colleague's LINQ code behaves strangely: a filtered list seems to change after it was built, a database query throws ObjectDisposedException, logging shows the same query running three times, and an exception appears far from the line that caused it. Explain deferred execution and how you would debug and fix each symptom.

    Deferred execution in practice: when queries run, captured variables, multiple enumeration, disposed contexts, late exceptions, and the tools and rules that prevent these bugs.

    Mid-Level Debugging LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  6. Which LINQ operators cause immediate execution, and how do you choose between ToList, ToArray, ToDictionary, ToHashSet, ToLookup and the async versions? Explain the cost of each, single-item operators like First, Single and Any, and how to materialise efficiently.

    Immediate execution in LINQ: scalar and collection operators, First versus Single versus Any, choosing the right materialisation, async materialisation in EF Core, and allocation costs.

    Junior Theory LINQ Β· πŸ”— 2 follow-ups
  7. What is IEnumerable<T> really? Explain the IEnumerable/IEnumerator contract, how foreach is compiled (including the pattern-based GetEnumerator and struct enumerators), enumerator disposal, IAsyncEnumerable<T>, and when a method should accept or return IEnumerable<T>.

    IEnumerable<T> and IEnumerator<T> in depth: the contract, foreach lowering, struct enumerators and boxing, disposal, async streams, and API design guidance on accepting and returning sequences.

    Mid-Level Theory LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  8. What is IQueryable<T>, and how is it different from IEnumerable<T>? Explain Expression, ElementType and Provider, how Queryable methods build expression trees, why the static type of a variable decides where a query runs, AsEnumerable and AsQueryable, and whether a repository should expose IQueryable.

    IQueryable<T>: the provider model, Queryable versus Enumerable overload binding, crossing between database and memory with AsEnumerable, and the design debate about exposing IQueryable.

    Senior Theory LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  9. What are expression trees in C#? Explain how the compiler turns a lambda into an Expression<TDelegate>, how to build, inspect and compile expression trees at run time, how LINQ providers and libraries use them, what C# features cannot appear in them, and the performance and AOT costs of Compile().

    Expression trees: code as data, compiler-generated trees from lambdas, building and visiting trees, combining predicates, Compile cost and caching, interpretation under Native AOT, and language limitations.

    Senior Coding LINQ Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  10. Code review: this order-reporting code mixes EF Core and LINQ to Objects. It calls ToList() before Where, uses Count() > 0, calls FirstOrDefault on a list inside a loop, does OrderBy().First(), enumerates the same query several times, loads related data inside a foreach, and builds IN lists with Contains over 50,000 ids. Find the LINQ performance mistakes and rewrite it.

    A code review of the most common LINQ performance mistakes in .NET 10 services, in memory and against EF Core, with the reasoning and the rewrite for each.

    Senior CodeReview LINQ Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  11. How are extension methods resolved and when should you use them? Explain how the compiler binds them, why instance methods always win, null receivers, ref extension methods on structs, the C# 14 extension block syntax for extension properties and static members, and the design mistakes extension methods invite.

    Extension methods beyond the basics: binding and precedence rules, namespace scoping, null receivers, ref this for structs, C# 14 extension members, and good versus bad uses.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  12. What are anonymous types in C#? Explain what the compiler generates, how equality and immutability work, why == behaves differently from Equals, when two anonymous types share a type, their use in LINQ projections and EF Core, and their limits compared with tuples and records.

    Anonymous types: compiler-generated internal classes, value equality via Equals but reference equality via ==, type unification, with-expressions, LINQ and EF Core projections, and when to switch to records or tuples.

    Junior Theory Advanced Language Features Β· πŸ”— 2 follow-ups
  13. How do tuples work in C#? Explain System.ValueTuple versus System.Tuple, element names and why they disappear at run time, tuple equality, mutability, performance, serialisation pitfalls, and when you should use a record instead.

    C# tuples: ValueTuple internals, compile-time names via TupleElementNames, equality, mutable fields, boxing and size, JSON serialisation gotchas, and choosing tuples versus records.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  14. What is deconstruction in C#? Explain how var (a, b) = x works for tuples, records and any type with a Deconstruct method (including extension Deconstruct), discards, deconstruction in foreach and LINQ, mixed declaration and assignment, and how deconstruction relates to positional patterns.

    Deconstruction: tuple and Deconstruct-based unpacking, extension Deconstruct for types you do not own, discards, foreach over dictionaries, C# 10 mixed forms, and the link to positional patterns.

    Junior Theory Advanced Language Features Β· πŸ”— 3 follow-ups
  15. What kinds of pattern matching does C# 14 support? Explain type, constant, relational, logical, property (including extended property), positional, var, discard and list patterns, how the compiler checks exhaustiveness and subsumption, and when pattern matching is better or worse than polymorphism.

    The full C# pattern language: every pattern kind with its version, how patterns compose, compiler exhaustiveness and subsumption checks, performance of the generated decision tree, and design trade-offs versus virtual methods.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  16. How do switch expressions differ from switch statements in C#? Explain arms, guards, discards, target typing of the result, exhaustiveness and SwitchExpressionException, tuple switches for state machines, and when a switch expression is the wrong tool.

    Switch expressions in depth: syntax and semantics versus switch statements, when clauses, natural and target typing, exhaustiveness checking, tuple-based state machines and readability trade-offs.

    Mid-Level Coding Advanced Language Features Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  17. A colleague wrote an iterator method and an async method that validate their arguments, but ArgumentNullException is only thrown when the result is enumerated or awaited, far from the bad call. Why does this happen, and how do local functions fix it? Also explain static local functions and when to prefer a local function over a lambda or private method.

    Local functions in practice: eager validation for iterators and async methods, capture without allocation, static local functions, recursion, and choosing between local functions, lambdas and private methods.

    Mid-Level Debugging Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  18. How do iterators work in C#? Explain yield return and yield break, what state machine the compiler generates, lazy evaluation, how try/finally and using behave inside iterators, the restrictions on iterator methods, async iterators, and the performance trade-offs of yield.

    Iterators: yield return and yield break semantics, compiler-generated state machines, disposal and finally, restrictions (no ref params, no yield in catch), async iterators, and cost versus materialised collections.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  19. What are closures in C#? Explain how captured variables are hoisted into a compiler-generated class, why capture is by variable rather than by value, the for-loop versus foreach capture difference, closures in async code and timers, the allocation and memory-leak risks, and how static lambdas help.

    Closures in C#: display classes, capture by variable, loop capture rules since C# 5, shared closure scopes, async and long-lived closures, allocation costs and leaks, and mitigation with static lambdas and state arguments.

    Mid-Level Debugging Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  20. How does reflection work in .NET and what does it cost? Explain Type, MemberInfo and BindingFlags, why MethodInfo.Invoke is slow, how to make reflection fast (cached delegates, MethodInvoker in .NET 8, UnsafeAccessor), and why reflection breaks trimming and Native AOT unless you annotate it.

    Reflection APIs, their run-time cost, faster alternatives in .NET 8+, and trimming/AOT annotations.

    Senior Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world

🌐 Sangam's Webring

βͺ Previous 🏠 Main Next ⏩
πŸ’¬ Chat with me