📁 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.

Advanced Language Features

Extension methods, anonymous types, tuples and deconstruction, pattern matching and switch expressions, local functions, iterators and yield, closures, reflection, attributes, source generators and Roslyn analysers.

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. How do attributes work in C#? Explain what the compiler stores in metadata, when attribute instances are actually created, AttributeUsage (targets, AllowMultiple, Inherited), allowed argument types, generic attributes (C# 11), attributes the compiler itself acts on, and caller information attributes including CallerArgumentExpression.

    Attributes as metadata, custom attribute design, generic attributes, compiler-interpreted attributes and caller info attributes.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  12. As a consumer, how do you use the source generators that ship with .NET 10? Explain what a source generator can and cannot do, and walk through System.Text.Json source generation, GeneratedRegex, LoggerMessage, LibraryImport, the configuration binding and Request Delegate generators, and how to inspect and debug the generated code.

    Using built-in .NET source generators for performance, trimming and Native AOT, and inspecting generated code.

    Mid-Level Theory Advanced Language Features Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  13. What are Roslyn analysers and how do you use them to enforce rules in a team? Explain the compiler-as-a-platform idea (syntax trees, semantic model, operations), how built-in .NET analysers and code style rules are configured, BannedApiAnalyzers, and walk through writing and packaging a small custom DiagnosticAnalyzer.

    Roslyn analysers in practice: configuration, banned APIs, and a minimal custom DiagnosticAnalyzer.

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

🌐 Sangam's Webring

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