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
- Explain the difference between mocking and stubbing in the context of testing. When should you prefer one over the other?
- 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.
- 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?
- 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?
- Describe scenarios where the Adapter pattern is particularly beneficial in a .NET application. What are some common pitfalls to avoid when implementing it?
- A project fails to restore with an error indicating a circular dependency was detected between `PackageA` and `PackageB`. How must this issue be resolved?
- Analyze the following test code and determine its correctness for testing exceptions with Fluent Assertions.
- Describe strategies for improving the performance of EF Core transactions, particularly in scenarios involving large datasets or complex operations.
- If an `OperationCanceledException` is thrown because a `CancellationToken` was signaled, what state is the `Task` in?
- How can you implement refresh tokens to improve the security and usability of your JWT-based authentication system?
- 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?
- 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?
- Assuming the JavaScript function `doSomething` expects two arguments (`name`, `value`), why will this JS Interop call fail at runtime?
- In the following code, an `async` method is called without being awaited. What is the primary issue with this approach to exception handling?
- The following asynchronous method is intended to handle potential exceptions from an external service call. Identify the primary issue with this exception handling strategy.
- 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?
- Identify the potential thread safety issue in the following code snippet. Explain why the original code is flawed.
- Analyze the following code. Why might it yield a low mutation score despite having tests for positive numbers, negative numbers, and division by zero?
- A UI application becomes unresponsive after a button click that calls `GetDataAsync()`. Which of the following best explains the likely cause of the deadlock?
- Assuming the `UpdateUIAsync` method is called from a background thread (`Task.Run`), identify the bug in this code.
- 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?
- 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.
- Compare and contrast three major .NET profiling tools or toolsets. For each, describe its strengths and an ideal use case.
- 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.
- 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?
- 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?
- Describe a scenario where using `CancellationTokenSource.CancelAfter` might be problematic and how you would address it.
- 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.
- Describe best practices for designing and using DTOs in a large-scale ASP.NET Core application. Discuss considerations for maintainability, scalability, and performance.
- Describe a scenario where you would need to use a custom `IServiceProvider` implementation in ASP.NET Core, and explain the implementation details.
- Describe a scenario where using `IAsyncEnumerable<T>` might be less efficient than a different approach, and explain why. Suggest an alternative.
- 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?
- 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.
- 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.
- Compare the performance characteristics and use cases of `volatile` versus the `lock` statement in C# for ensuring memory visibility.
- 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.
- 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.
- 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?
- Database deadlocks spike after adding parallelism. Which diagnostics and retries help?
- Describe how to ensure proper resource cleanup (e.g., closing database connections, disposing of streams) in complex async methods, regardless of success or failure.
- Bulk imports take minutes when inserting millions of rows. How can `SqlBulkCopy` or EFCore.BulkExtensions help while staying transactional?
- Compare the performance implications of chaining multiple LINQ operators on an `IEnumerable<T>` versus materializing the result at each step into a `List<T>`.
- Describe a scenario where using `Parallel.ForEach` might lead to unexpected behavior or performance issues, and explain how to mitigate them.
- Database CPU spikes when loading aggregates with multiple navigation properties. How can split queries alleviate SQL Server plan bloat?
- 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.
- 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?
- Backpressure is killing downstream services when using observables for telemetry. How do you buffer or window events safely?
- Compare and contrast optimistic and pessimistic concurrency control strategies in EF Core. When would you choose one over the other?
- Describe different strategies for optimizing the performance of LINQ queries that operate on large in-memory `IEnumerable<T>` collections.
- Compare orchestrated and choreographed sagas for order fulfillment.