📁 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 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
  2. 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
  3. 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
  4. Review this code from a project that has just enabled nullable reference types: it is full of `!` operators, `= default!` on properties, and helper methods like TryGetCustomer that the compiler does not understand. What is wrong, and how do you make nullable annotations actually protect you? Cover the nullable attributes, generics with T?, EF Core and deserialisation.

    Advanced nullable reference types: code review of common misuse, nullable attributes, generics and framework boundaries.

    Mid-Level CodeReview Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  5. How do init and required work together in C#? Explain init-only setters (C# 9), required members (C# 11), what the compiler enforces, SetsRequiredMembers, how they interact with constructors, object initialisers, with expressions and System.Text.Json, and when you would choose a constructor instead.

    init-only setters and required members: object initialisation contracts, constructors, with expressions and deserialisation.

    Mid-Level Theory Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  6. How do you use records well in production C#? Explain what the compiler generates for a record class (equality, EqualityContract, ToString, Deconstruct, copy constructor), how inheritance affects equality, why with is a shallow copy, the collection-equality trap, and where records fit or do not fit (DTOs, value objects, EF Core entities).

    Practical record class usage: generated members, inheritance, with expressions, collection equality and EF Core fit.

    Mid-Level Theory Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  7. What is a record struct in C# 10, and how does it differ from a record class and a plain struct? Explain readonly record struct, the mutability default, generated equality versus ValueType.Equals, with expressions on structs, the default value problem, and when a record struct is the right choice.

    record struct and readonly record struct: semantics, equality performance, default values and when to choose them.

    Mid-Level Theory Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  8. How do primary constructors on classes and structs (C# 12) differ from those on records? Explain parameter capture, why parameters are not properties, the double-storage warning CS9124, mutation of captured parameters, validation, use with dependency injection, and the pitfalls you would flag in review.

    C# 12 primary constructors for classes and structs: capture semantics, warnings, DI usage and review pitfalls.

    Mid-Level CodeReview Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  9. What are file-scoped namespaces, global using directives and implicit usings in modern C#? Explain how each works, where to put global usings, how ImplicitUsings and the Using MSBuild item generate them, global using static and alias directives (including C# 12 alias any type), and the problems they can cause in large solutions.

    File-scoped namespaces (C# 10), global usings, implicit usings and using aliases in modern projects.

    Junior Theory Modern C# Β· πŸ”— 2 follow-ups
  10. What are raw string literals in C# 11? Explain the triple-quote syntax, how indentation is removed, including quotes inside the text, interpolated raw strings with multiple dollar signs, UTF-8 raw strings, and where they help in real code such as JSON, SQL and test fixtures.

    Raw string literals: syntax, indentation rules, interpolation with $$ and practical uses.

    Junior Theory Modern C# Β· πŸ”— 2 follow-ups
  11. What are collection expressions in C# 12? Explain the [a, b, ..c] syntax, which target types they support (arrays, spans, lists, interfaces, CollectionBuilder types), why they have no natural type, how the compiler chooses an efficient implementation, the spread operator, and the performance and API design implications.

    Collection expressions: syntax, target typing, CollectionBuilder, spread and performance behaviour.

    Mid-Level Theory Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  12. How do list patterns work in C# 11? Explain matching on length and elements, the discard and slice patterns, capturing a slice with .. var rest, which types support list patterns (countable and indexable, plus slicing support), the allocation cost of slices for arrays, lists, strings and spans, and real uses such as command parsing.

    List patterns: syntax, slice patterns, type requirements, slice costs and practical uses.

    Mid-Level Coding Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  13. What are interceptors in C#, and what should a .NET developer know about them? Explain what problem they solve, how a source generator uses InterceptsLocation to redirect a specific call site, the InterceptorsNamespaces opt-in, where .NET already uses them (Request Delegate Generator, configuration binding generator), and why you should not hand-write them.

    Awareness of interceptors: purpose, mechanics, opt-in, built-in uses and limits.

    Senior Theory Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  14. Write generic numeric code in C# using static abstract interface members and generic math (.NET 7+). Explain INumber<T> and the smaller interfaces, T.Zero and T.CreateChecked, IParsable<T>, how to make your own type participate (operators as static abstract members, the self-referencing constraint), checked operators, and the performance and design trade-offs.

    Generic math with static abstract interface members: writing and consuming numeric generic code.

    Senior Coding Modern C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  15. What is `Span<T>` and how do you use it to write allocation-free code? Explain what a span points to (arrays, stack memory, native memory), slicing, why it is a ref struct and what that forbids, MemoryExtensions helpers, CollectionsMarshal.AsSpan, and the performance gains and risks.

    Span<T> in depth: what it can point to, slicing, ref struct limits, helper APIs and performance.

    Mid-Level Coding Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  16. When should you use `ReadOnlySpan<T>` instead of `Span<T>`, and what modern features build on it? Explain read-only views over strings and constant data, u8 literals, static ReadOnlySpan properties backed by assembly data, SearchValues, overload preference, and the C# 14 first-class span conversions.

    ReadOnlySpan<T>: read-only views, constant data, u8 literals, SearchValues and C# 14 span conversions.

    Mid-Level Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  17. What are `Memory<T>` and `ReadOnlyMemory<T>`, and how do they differ from spans? Explain why they can live on the heap and cross await, how .Span works, slicing, getting at the underlying array with MemoryMarshal.TryGetArray, ownership and lifetime rules, and how they appear in Stream, PipeReader and System.Text.Json APIs.

    Memory<T>/ReadOnlyMemory<T>: heap-storable buffers for async code, ownership and API usage.

    Mid-Level Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  18. Explain ref locals, ref returns and ref fields in C#. How do ref safety rules and the scoped keyword work, what does CollectionsMarshal.GetValueRefOrAddDefault give you, when do ref returns improve performance, and what bugs can references to storage cause?

    ref locals, ref returns, ref fields (C# 11), scoped, ref safety and practical uses.

    Senior Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  19. How do out parameters work in C#, and when should you use them? Explain definite assignment, out var and discards, the Try pattern, how out relates to ref under the hood, why async methods and iterators cannot have out parameters, nullable annotations for Try methods, and when a tuple or result type is a better choice.

    out parameters: semantics, Try pattern, limits and alternatives such as tuples and result types.

    Junior Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups
  20. What do `in` parameters and `ref readonly` parameters (C# 12) do? Explain passing large structs by readonly reference, hidden defensive copies with non-readonly structs, readonly members, when in makes code slower, the call-site rules for in and ref readonly, and how to decide between by-value, in and ref readonly.

    in and ref readonly parameters: readonly references, defensive copies and when they help or hurt.

    Senior Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world

🌐 Sangam's Webring

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