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

πŸ“š .NET Interview Questions

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.

Creational Patterns

Singleton, Factory Method, Abstract Factory, Builder, Prototype and Object Pool, and how DI lifetimes, factories and pooling replace or implement them in .NET 10.

  1. What are design patterns? Explain the GoF split into creational, structural and behavioural patterns, give a .NET example of each, and tell me when using a pattern makes code worse.

    What design patterns are, the three GoF categories with .NET framework examples (DI lifetimes, IHttpClientFactory, StringBuilder, Stream wrappers, DelegatingHandler, IEnumerable, middleware), and when a pattern is ceremony.

    Beginner Theory Creational Patterns Β· πŸ”— 2 follow-ups
  2. How do you implement a thread-safe Singleton in C#? Compare a static readonly field, Lazy<T> and double-checked locking, and explain why in an ASP.NET Core app you would normally register a singleton in DI instead.

    Singleton intent, three thread-safe C# implementations (static readonly with type initialiser guarantees, Lazy<T> with ExecutionAndPublication, double-checked locking with volatile), their pitfalls, and why the DI singleton lifetime replaces the hand-rolled pattern.

    Junior Theory Creational Patterns Β· πŸ”— 3 follow-ups
  3. Your ASP.NET Core API sometimes returns another user's data, and after a refactor startup fails with "Cannot consume scoped service 'AppDbContext' from singleton". What is a captive dependency, how do you diagnose and fix it, and how do ValidateScopes and ValidateOnBuild help?

    Captive dependencies (a singleton holding a scoped or transient service), the data-leak and thread-safety symptoms, ValidateScopes and ValidateOnBuild, and fixes with IServiceScopeFactory, lifetime changes and IHttpClientFactory.

    Mid-Level Debugging Creational Patterns Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  4. What is the difference between a simple factory, the Factory Method pattern and the Abstract Factory pattern? Give a .NET framework example of each and explain when DI makes a hand-written factory unnecessary.

    Simple factory vs GoF Factory Method (subclass decides, DbConnection.CreateCommand) vs Abstract Factory (families of related objects, DbProviderFactory, DbDataSource), IHttpClientFactory and ILoggerFactory, and when DI factory delegates replace them.

    Intermediate Theory Creational Patterns Β· πŸ”— 3 follow-ups
  5. You have several payment gateways (Stripe, PayPal, a bank API) and the right one depends on the order. Show how you would select the implementation at run time in ASP.NET Core using DI: factory delegates, keyed services with [FromKeyedServices], and an IEnumerable-based resolver. How do you avoid the service locator anti-pattern?

    Run-time implementation selection in .NET 10 DI: keyed services (AddKeyedScoped, [FromKeyedServices], GetRequiredKeyedService), Func factory delegates, an IEnumerable-to-FrozenDictionary resolver, unknown-key handling, testing, and keeping IServiceProvider at the composition root.

    Mid-Level Coding Creational Patterns Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  6. What is the Builder pattern? Explain fluent builders, how WebApplicationBuilder and HostApplicationBuilder use it, test data builders, and when C# required and init properties with an object initialiser are enough instead.

    GoF Builder vs fluent builder, framework builders (StringBuilder, UriBuilder, SqlConnectionStringBuilder, WebApplicationBuilder, HostApplicationBuilder, DbContextOptionsBuilder, ResiliencePipelineBuilder), test data builders, validation in Build(), and when required/init initialisers replace a builder.

    Intermediate Theory Creational Patterns Β· πŸ”— 2 follow-ups
  7. What is the Prototype pattern? Explain shallow versus deep copies, how MemberwiseClone works, why ICloneable is discouraged, and why a record's with expression can still surprise you.

    Prototype intent, shallow vs deep copy, MemberwiseClone semantics, ICloneable's ambiguity, records and with (shallow copy constructor), deep-copy options, JsonSerializerOptions and HttpRequestMessage cloning, and EF Core PropertyValues.Clone.

    Mid-Level Theory Creational Patterns Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  8. When does the Object Pool pattern help in .NET, and when does it make things slower? Explain ObjectPool<T>, ArrayPool<T>, DbContext pooling and connection pooling, the bugs that come from resetting pooled objects badly, and how you would prove a pool is worth it.

    Object Pool in .NET 10: Microsoft.Extensions.ObjectPool (policies, IResettable, MaximumRetained), ArrayPool<T>.Shared, AddDbContextPool, ADO.NET connection pooling, reset and use-after-return bugs, why pooling can be slower than gen0 allocation, and measuring with BenchmarkDotNet and dotnet-counters.

    Senior Theory Creational Patterns Β· πŸ”— 2 follow-ups Β· πŸ›  1 real-world
  9. Code review: a CurrencyService exposes a hand-rolled static Instance, caches rates in a plain Dictionary, creates new HttpClient() and new AppDbContext() itself, calls .Result on async code, and picks a rate provider with a switch on a config string. What is wrong, in order of severity, and how would you rewrite it with DI?

    Code review of creational anti-patterns: racy static singleton, unsynchronised Dictionary, HttpClient per call, DbContext held by a singleton, sync-over-async, switch factory with silent fallback and hard-coded secret; rewrite with DI lifetimes, typed HttpClient, HybridCache, keyed or options-driven provider selection.

    Senior CodeReview Creational Patterns Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world

🌐 Sangam's Webring

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