1325 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.
Strategy, Observer, Command, Mediator, Chain of Responsibility, State, Visitor, Template Method, Iterator, Memento, Specification and Null Object in modern C#.
Strategy intent and structure, replacing growing switch statements with interchangeable algorithms, .NET examples (IComparer<T>, IEqualityComparer<T>, StringComparer, JsonNamingPolicy), selecting strategies via IEnumerable<T>, a dictionary or keyed services, delegates as one-method strategies, and when a switch is still clearer.
Step-by-step refactor of a carrier switch into keyed shipping strategies: characterisation tests first, IShippingStrategy with a Carrier key, rates moved to options, a context that builds a dictionary once and fails fast on duplicates, DI registration, unit and registration tests, Open/Closed in practice, and when a switch is the better answer.
Observer intent and structure, C# events and EventHandler<T>, IObservable<T>/IObserver<T> with IDisposable subscriptions and Rx, IOptionsMonitor<T>.OnChange and change tokens, INotifyPropertyChanged, unsubscribing and the leak risk, synchronous in-process notification vs broker-based pub/sub.
Observer lifetime mismatch in DI: a scoped subscriber to a singleton subject (event and IOptionsMonitor.OnChange) stays reachable through the delegate invocation list; proving it with dotnet-counters, two dotnet-gcdump snapshots and paths to root; fixing it with IDisposable unsubscription the container calls, moving subscriptions to singletons, lambda pitfalls, weak references, and a WeakReference regression test.
Command intent and structure (invoker, command, receiver), GoF Execute/Undo commands for undo stacks and macros, modern serialisable command records with ICommandHandler<T>, queuing with Channel<T> or a broker, retries and idempotency keys, audit logs, WPF/MAUI ICommand, and commands vs events.
GoF Mediator (colleagues talk through a mediator) vs MediatR as an in-process request dispatcher, IRequest/IRequestHandler, INotification, IPipelineBehavior for validation and logging, costs (indirection, navigation, reflection), MediatR's July 2025 move to dual RPL-1.5/commercial licensing under Lucky Penny Software and the Community edition, and alternatives: direct handler injection, decorators, source-generated mediators.
Chain of Responsibility intent and structure, the classic handle-or-pass chain (approval limits) vs the pipeline variant where every handler can act before and after next, ASP.NET Core middleware (RequestDelegate next), DelegatingHandler, short-circuiting, the recommended middleware order, and the costs of long implicit chains.
State pattern applied to an order aggregate: (state, trigger) switch-expression transition tables, State classes when behaviour differs per state, the Stateless library (5.20.1) with Permit/PermitIf, external state storage, OnEntry, CanFire and graph export, returning 409 for invalid transitions, persisting the status with EF Core and a rowversion concurrency token, and testing transitions.
Visitor intent, Accept/Visit double dispatch, the expression problem (adding types vs adding operations), ExpressionVisitor in LINQ providers and EF Core, Roslyn CSharpSyntaxWalker and CSharpSyntaxVisitor<T>, pattern-matching switch expressions over record hierarchies as the lighter alternative, exhaustiveness limits in C# 14, costs, and when a classic visitor still wins.
Template Method intent and structure, non-virtual skeleton with abstract steps and virtual hooks (NVI idiom), framework examples (BackgroundService.ExecuteAsync and its .NET 10 behaviour change, AuthenticationHandler<T>, Collection<T>, DbContext.OnModelCreating, JsonConverter<T>), the fragile base class problem, virtual calls in constructors, and replacing inheritance with strategies or delegates.
Iterator intent (traverse without exposing structure), IEnumerable<T>/IEnumerator<T> roles, foreach pattern binding, external vs internal iteration, yield return for custom traversals (tree walking), IAsyncEnumerable<T> for paged APIs with await foreach and [EnumeratorCancellation], LINQ over async streams in .NET 10, and gotchas: deferred execution, multiple enumeration, modifying during enumeration and disposal.
Memento intent and roles (originator, memento, caretaker), opaque snapshots via a private nested type behind a marker interface, immutable records and with-expressions as free snapshots, structural sharing with ImmutableList<T>, memory costs and capping history, Memento vs Command undo, persisting and versioning snapshots, and snapshots in event sourcing.
Specification pattern in C#: Specification<T> exposing Expression<Func<T,bool>>, And/Or/Not composition with an ExpressionVisitor parameter replacer, in-memory IsSatisfiedBy, translation to SQL by EF Core, repository-method explosion, Ardalis.Specification with criteria, ordering, paging and WithSpecification, the Func<T,bool> vs Expression trap that loads a whole table, and testing translation with ToQueryString and integration tests.
Null Object intent (a do-nothing implementation instead of null), framework examples (NullLogger<T>.Instance, NullLoggerFactory, Stream.Null, TextWriter.Null, Array.Empty<T>, Enumerable.Empty<T>, Task.CompletedTask, CancellationToken.None), returning empty collections, optional dependencies in DI, comparison with nullable reference types and the Maybe/Result approach, and when a null object masks failures.
Senior code review of an order processor: static event leak from a scoped service, a home-made mediator resolving scoped handlers from the root provider and invoking async handlers via reflection (lost exceptions), cancelled orders processed through direct status writes, events lost without an outbox, pricing if/else chains; ranking by severity, choosing Strategy and a switch-table State, rejecting overkill patterns, the rewrite, and how to close the review.