Top .NET Interview Questions

Top 50 .NET interview questions with curated answers across core concepts, practical scenarios, and interview-ready fundamentals.

Top 50 .NET Interview Questions

  1. Explain the difference between mocking and stubbing in the context of testing. When should you prefer one over the other?
  2. Explain how the choice of garbage collection (GC) mode in .NET (e.g., Workstation GC vs. Server GC) can impact the performance of a profiling session itself.
  3. A base class has a constructor that initializes a critical resource. How do you guarantee the derived class correctly initializes this resource before its own initialization begins?
  4. How can the Decorator pattern be implemented using dependency injection in ASP.NET Core to add functionality to an existing service without modifying its core code?
  5. Describe scenarios where the Adapter pattern is particularly beneficial in a .NET application. What are some common pitfalls to avoid when implementing it?
  6. A project fails to restore with an error indicating a circular dependency was detected between `PackageA` and `PackageB`. How must this issue be resolved?
  7. Analyze the following test code and determine its correctness for testing exceptions with Fluent Assertions.
  8. Describe strategies for improving the performance of EF Core transactions, particularly in scenarios involving large datasets or complex operations.
  9. If an `OperationCanceledException` is thrown because a `CancellationToken` was signaled, what state is the `Task` in?
  10. How can you implement refresh tokens to improve the security and usability of your JWT-based authentication system?
  11. An authorization handler accesses a DbContext. What is a potential concurrency issue with this code if the DbContext were incorrectly registered as a singleton service?
  12. A policy has two requirements, A and B, each with its own handler. What is a potential bug in the handler for requirement A if it doesn't explicitly fail when its condition is not met?
  13. Assuming the JavaScript function `doSomething` expects two arguments (`name`, `value`), why will this JS Interop call fail at runtime?
  14. In the following code, an `async` method is called without being awaited. What is the primary issue with this approach to exception handling?
  15. The following asynchronous method is intended to handle potential exceptions from an external service call. Identify the primary issue with this exception handling strategy.
  16. In the following asynchronous method, if both tasks fail, an exception will be unhandled. What is the correct way to handle exceptions from both tasks?
  17. Identify the potential thread safety issue in the following code snippet. Explain why the original code is flawed.
  18. Analyze the following code. Why might it yield a low mutation score despite having tests for positive numbers, negative numbers, and division by zero?
  19. A UI application becomes unresponsive after a button click that calls `GetDataAsync()`. Which of the following best explains the likely cause of the deadlock?
  20. Assuming the `UpdateUIAsync` method is called from a background thread (`Task.Run`), identify the bug in this code.
  21. Describe an alternative to in-memory databases for integration testing in ASP.NET Core that provides even higher fidelity. What are the tradeoffs of this approach?
  22. Compare and contrast two common strategies for managing database state in ASP.NET Core integration tests: using EF Core's In-Memory provider versus using SQLite in-memory mode.
  23. Compare and contrast three major .NET profiling tools or toolsets. For each, describe its strengths and an ideal use case.
  24. Describe a scenario where using `async` and `await` in C# might inadvertently lead to a deadlock, even without explicit locks. Explain how this can happen and how to prevent it.
  25. Compare the performance implications of one-way vs. two-way data binding in Blazor. In what scenarios could excessive use of two-way binding lead to performance issues?
  26. Compare and contrast using a `CascadingValue` versus a singleton service for sharing application-wide data like user information. What are the advantages and disadvantages of each approach?
  27. Describe a scenario where using `CancellationTokenSource.CancelAfter` might be problematic and how you would address it.
  28. Describe best practices for using concurrent collections in C# to avoid common pitfalls like deadlocks, race conditions, and performance bottlenecks. Provide code examples to illustrate.
  29. Describe best practices for designing and using DTOs in a large-scale ASP.NET Core application. Discuss considerations for maintainability, scalability, and performance.
  30. Describe a scenario where you would need to use a custom `IServiceProvider` implementation in ASP.NET Core, and explain the implementation details.
  31. Describe a scenario where using `IAsyncEnumerable<T>` might be less efficient than a different approach, and explain why. Suggest an alternative.
  32. Compare and contrast how Bicep and Terraform handle state management. What are the pros and cons of Bicep's reliance on Azure Resource Manager versus Terraform's explicit state file?
  33. Describe a scenario where you would use middleware to implement custom authentication logic in an ASP.NET Core application. Explain your design and key considerations.
  34. Compare and contrast the testing strategies for Minimal APIs versus Controller-based APIs in ASP.NET Core. Discuss the tools and techniques best suited for each.
  35. Compare the performance characteristics and use cases of `volatile` versus the `lock` statement in C# for ensuring memory visibility.
  36. Describe how recursive patterns can be used in C# to process nested or self-referential data structures, such as a tree. Provide an example and discuss potential performance considerations.
  37. Compare and contrast using a C# record versus a `ValueTuple` for returning multiple values from a method. Discuss the trade-offs in terms of performance, readability, and use case.
  38. Besides a dedicated join entity, how can you configure a many-to-many relationship in EF Core 5+ and what are the benefits of this approach?
  39. Database deadlocks spike after adding parallelism. Which diagnostics and retries help?
  40. Describe how to ensure proper resource cleanup (e.g., closing database connections, disposing of streams) in complex async methods, regardless of success or failure.
  41. Bulk imports take minutes when inserting millions of rows. How can `SqlBulkCopy` or EFCore.BulkExtensions help while staying transactional?
  42. Compare the performance implications of chaining multiple LINQ operators on an `IEnumerable<T>` versus materializing the result at each step into a `List<T>`.
  43. Describe a scenario where using `Parallel.ForEach` might lead to unexpected behavior or performance issues, and explain how to mitigate them.
  44. Database CPU spikes when loading aggregates with multiple navigation properties. How can split queries alleviate SQL Server plan bloat?
  45. Besides locks and `Interlocked`, describe how to use thread-local storage in a `Parallel.For` loop to safely calculate an aggregate sum without causing data races.
  46. A read-heavy dashboard repeatedly executes the same EF Core LINQ query with only parameter differences. How do you leverage compiled queries to cut CPU usage?
  47. Backpressure is killing downstream services when using observables for telemetry. How do you buffer or window events safely?
  48. Compare and contrast optimistic and pessimistic concurrency control strategies in EF Core. When would you choose one over the other?
  49. Describe different strategies for optimizing the performance of LINQ queries that operate on large in-memory `IEnumerable<T>` collections.
  50. Compare orchestrated and choreographed sagas for order fulfillment.