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

Memory-Oriented C#

Span, ReadOnlySpan, Memory and ReadOnlyMemory, ref, out, in, params, readonly, ref struct, stackalloc, unsafe code, ArrayPool and MemoryPool.

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. How do params work in modern C#, and what changed with C# 13 params collections? Explain params arrays and their hidden allocations, params ReadOnlySpan<T>, params IEnumerable<T> and List<T>, how overload resolution chooses between them, the new BCL overloads in .NET 9, and API design guidance.

    params arrays versus C# 13 params collections, allocations, overload resolution and API design.

    Mid-Level Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  8. What does readonly mean in each place it can appear in C#? Explain readonly fields, static readonly versus const (and the versioning trap), readonly structs, readonly members, readonly ref returns, how the JIT treats static readonly values, and why readonly does not make an object immutable.

    readonly across fields, structs, members and refs, const versus static readonly, and immutability limits.

    Mid-Level Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  9. What is a ref struct and when would you write one? Explain the rules (stack-only, no boxing, no class fields, no lambda capture), ref fields (C# 11), C# 13 changes (implementing interfaces, the allows ref struct anti-constraint, use in async and iterator methods), the Dispose pattern for ref structs, and walk through a ValueStringBuilder-style type.

    Writing ref structs: rules, ref fields, C# 13 interface and generic support, and a stack-first builder example.

    Senior Coding Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  10. How do you use stackalloc safely in modern C#? Explain stackalloc into Span<T>, the unmanaged type requirement, stack size limits and why a stack overflow kills the process, the hybrid stackalloc-or-ArrayPool pattern, why you must not stackalloc in loops, SkipLocalsInit, and how to choose a threshold.

    Safe stackalloc: span usage, size limits, hybrid pooling pattern, loops and SkipLocalsInit.

    Mid-Level Coding Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  11. When is unsafe code justified in modern C#, and what tools exist beyond raw pointers? Explain unsafe blocks, pointers and fixed (pinning), function pointers, NativeMemory, the System.Runtime.CompilerServices.Unsafe class, MemoryMarshal, what can go wrong (GC holes, buffer overruns, pinning cost), and how to keep unsafe code safe.

    Unsafe C#: pointers, fixed, NativeMemory, the Unsafe class and MemoryMarshal, risks and guidelines.

    Senior Theory Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  12. A service using ArrayPool<T> intermittently returns corrupted data, and once returned another customer's data in a response. How do you debug and fix ArrayPool misuse? Cover use-after-return, double return, assuming the rented length, not clearing sensitive data, returning foreign arrays, and the tools and test techniques that catch these bugs.

    Debugging ArrayPool<T> misuse: lifetime bugs, length assumptions, clearing, diagnostics and safe patterns.

    Senior Debugging Memory-Oriented C# Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  13. How do MemoryPool<T> and IMemoryOwner<T> work, and when would you use them instead of ArrayPool<T>? Explain renting and disposing owners, why the buffer can be larger than requested, ownership transfer between components, how System.IO.Pipelines uses a memory pool, writing a custom pool or MemoryManager<T> for native or pinned memory, and common mistakes.

    MemoryPool<T> and IMemoryOwner<T>: ownership-based pooling, pipelines and custom memory managers.

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

🌐 Sangam's Webring

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