Top 25 .NET Interview Questions & Answers
Here are the top 25 .NET interview questions and their answers, covering C#, ASP.NET Core, Entity Framework, and related technologies.
1. What is the difference between .NET Framework, .NET Core, and .NET 5/6?
- .NET Framework: Windows-only, mature, not cross-platform.
- .NET Core: Cross-platform, open-source, modular, high performance.
- .NET 5/6+: Unified platform for Windows, Linux, macOS; combines best of both, with new features and performance improvements.
2. Explain the difference between value types and reference types in C#.
- Value types (e.g.,
int
,struct
) store data directly, usually on the stack. - Reference types (e.g.,
class
,string
,array
) store a reference to the data, which is on the heap. - Assigning a value type copies the value; assigning a reference type copies the reference.
3. What are async/await and how do they work?
-
async
andawait
are used for asynchronous programming. -
async
marks a method as asynchronous. -
await
pauses execution until the awaited task completes, without blocking the thread. -
Example:
public async Task<int> GetDataAsync() { var result = await SomeApiCall(); return result; }
4. Explain dependency injection in .NET Core.
- Dependency Injection (DI) is a design pattern where dependencies are provided to a class, not created by it.
- .NET Core has built-in DI. Register services in
Startup.cs
(orProgram.cs
in .NET 6+), then inject via constructor. - Improves testability and maintainability.
5. What are extension methods in C#?
-
Extension methods add new methods to existing types without modifying their source code.
-
Defined as static methods in a static class, with
this
before the first parameter.public static class StringExtensions { public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s); }
6. What is the difference between IEnumerable, ICollection, and IList?
Interface | Description |
---|---|
IEnumerable | Basic iteration over a collection |
ICollection | Adds count, add, remove, clear |
IList | Adds index-based access (get/set by index) |
7. Explain Entity Framework Core and how it works.
-
EF Core is an ORM (Object-Relational Mapper) for .NET.
-
Maps C# classes to database tables.
-
Supports LINQ queries, change tracking, migrations, and more.
-
Example usage:
var users = dbContext.Users.Where(u => u.IsActive).ToList();
8. What are delegates and events?
-
Delegate: Type-safe function pointer; can reference methods with a specific signature.
-
Event: Special delegate for publisher/subscriber scenarios (e.g., UI events).
-
Example:
public delegate void Notify(); public event Notify OnNotify;
9. Explain garbage collection in .NET.
- Automatic memory management system.
- Frees memory occupied by objects no longer in use.
- Uses generations (0, 1, 2) for optimization.
- Developers can force collection with
GC.Collect()
, but it’s not recommended.
10. What is middleware in ASP.NET Core?
-
Software components in the request pipeline.
-
Each middleware can process requests and responses.
-
Registered in
Startup.cs
(orProgram.cs
in .NET 6+). -
Example:
app.UseRouting(); app.UseAuthentication(); app.UseAuthorization();
11. What is the difference between Task and Thread?
- Thread: OS-level unit of execution.
- Task: Higher-level abstraction for asynchronous operations; can use threads from the thread pool.
- Tasks are preferred for async programming in .NET.
12. Explain LINQ with an example.
-
LINQ (Language Integrated Query) allows querying collections using C# syntax.
-
Example:
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
13. What are generics in C#?
-
Allow classes, interfaces, and methods to operate on types specified by the caller.
-
Provide type safety and reusability.
-
Example:
List<int> numbers = new List<int>();
14. What is the difference between abstract class and interface?
Feature | Abstract Class | Interface |
---|---|---|
Methods | Can have implementations | Only declarations (C# 8+ allows default) |
Fields | Can have fields | Cannot have fields |
Multiple Inherit. | No | Yes |
15. Explain RESTful API best practices in ASP.NET Core.
- Use proper HTTP verbs (GET, POST, PUT, DELETE).
- Return appropriate status codes.
- Use attribute routing.
- Validate input and handle errors gracefully.
- Secure endpoints (e.g., JWT, OAuth).
- Document APIs (Swagger/OpenAPI).
16. What are tuples in C#?
-
Tuples are lightweight data structures to group multiple values.
-
Example:
var person = (Name: "Alice", Age: 30); Console.WriteLine(person.Name); // Alice
17. Explain JWT authentication.
- JWT (JSON Web Token) is a compact, URL-safe token for authentication.
- Used for stateless authentication in APIs.
- Contains claims, is signed, and can be validated by the server.
18. What are nullable reference types?
-
Feature in C# 8+ to distinguish between nullable and non-nullable reference types.
-
Helps avoid
NullReferenceException
. -
Example:
string? name = null; // nullable string name2 = "not null"; // non-nullable
19. What are records in C# 9?
-
Immutable reference types for storing data.
-
Provide value-based equality.
-
Example:
public record Person(string Name, int Age);
20. Explain global exception handling in ASP.NET Core.
-
Use
UseExceptionHandler
middleware to catch unhandled exceptions. -
Can log errors and return custom error responses.
-
Example:
app.UseExceptionHandler("/Home/Error");
21. What is the difference between ConfigureServices and Configure in Startup.cs?
ConfigureServices
: Register services and dependencies (DI container).Configure
: Set up the HTTP request pipeline (middleware).
22. Explain options pattern in ASP.NET Core.
-
Used for accessing and managing configuration settings.
-
Bind configuration sections to strongly typed classes.
-
Example:
services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));
23. What is model binding and validation?
-
Model binding: Maps HTTP request data to action method parameters.
-
Validation: Uses data annotations to validate input.
-
Example:
public IActionResult Post([FromBody] UserModel model) { ... }
24. What is SignalR and where do you use it?
- SignalR is a library for real-time web functionality (e.g., chat, notifications).
- Supports WebSockets, Server-Sent Events, and Long Polling.
- Used in dashboards, live updates, collaborative apps.
25. Explain dependency lifetimes: Transient, Scoped, Singleton.
- Transient: New instance every time requested.
- Scoped: One instance per request.
- Singleton: One instance for the entire application lifetime.
Tip: Practice these questions and try to implement small code samples for each to deepen your understanding!