Below are 25 advanced .NET interview questions and answers tailored for experienced developers. These questions delve into deeper concepts and best practices in .NET and C#.
1. What is the difference between Task
and Thread
in C#?
Answer:
Thread
: A low-level construct for creating and managing threads. It is resource-intensive and harder to manage.Task
: A higher-level abstraction for asynchronous programming. It uses the thread pool and is easier to work with.
Example:
Task.Run(() => Console.WriteLine("Running on a thread pool thread"));
2. What is the async
and await
pattern in C#?
Answer:
The async
and await
keywords are used to write asynchronous code that is easy to read and maintain. async
marks a method as asynchronous, and await
pauses the method until the awaited task completes.
Example:
async Task<string> FetchDataAsync() {
return await HttpClient.GetStringAsync("https://example.com");
}
3. What is the difference between IEnumerable
and IQueryable
?
Answer:
IEnumerable
: Used for in-memory collections. Executes queries on the client side.IQueryable
: Used for querying data from databases. Executes queries on the server side.
Example:
IQueryable<Product> query = dbContext.Products.Where(p => p.Price > 100);
4. What is dependency injection (DI) in .NET?
Answer:
Dependency Injection is a design pattern that allows objects to receive dependencies from an external source rather than creating them internally. It promotes loose coupling and testability.
Example:
public class MyService {
private readonly ILogger _logger;
public MyService(ILogger logger) {
_logger = logger; // Dependency injected
}
}
5. What is the difference between AddTransient
, AddScoped
, and AddSingleton
in .NET Core DI?
Answer:
AddTransient
: A new instance is created every time it is requested.AddScoped
: A single instance is created per request.AddSingleton
: A single instance is created and shared throughout the application’s lifetime.
6. What is the difference between ActionResult
and IActionResult
in ASP.NET Core?
Answer:
ActionResult
: A concrete class that represents the result of an action method.IActionResult
: An interface that defines a contract for action results. It allows flexibility in returning different types of results.
7. What is middleware in ASP.NET Core?
Answer:
Middleware is software components that handle requests and responses in the ASP.NET Core pipeline. They are executed in the order they are added.
Example:
app.Use(async (context, next) => {
await context.Response.WriteAsync("Before\n");
await next();
await context.Response.WriteAsync("After\n");
});
8. What is the difference between ViewData
, ViewBag
, and TempData
in ASP.NET MVC?
Answer:
ViewData
: A dictionary for passing data from the controller to the view.ViewBag
: A dynamic wrapper aroundViewData
.TempData
: Used to pass data between actions (persists for one additional request).
9. What is the difference between Entity Framework
and Dapper
?
Answer:
- Entity Framework (EF): A full-featured ORM that provides high-level abstractions for database interactions.
- Dapper: A micro-ORM that focuses on performance and raw SQL queries.
10. What is the difference between First()
and FirstOrDefault()
in LINQ?
Answer:
First()
: Returns the first element in a sequence. Throws an exception if the sequence is empty.FirstOrDefault()
: Returns the first element or a default value (e.g.,null
) if the sequence is empty.
11. What is the difference between Any()
and Count()
in LINQ?
Answer:
Any()
: Returnstrue
if the sequence contains any elements. It is faster for checking existence.Count()
: Returns the number of elements in the sequence.
12. What is the difference between async void
and async Task
?
Answer:
async void
: Used for event handlers. It cannot be awaited and may cause unhandled exceptions.async Task
: Used for methods that return aTask
. It can be awaited and is preferred for asynchronous methods.
13. What is the difference between Task.Run()
and Task.Factory.StartNew()
?
Answer:
Task.Run()
: A simplified way to start a task on the thread pool.Task.Factory.StartNew()
: Provides more options for task creation (e.g., scheduling, cancellation).
14. What is the difference between StringBuilder
and String
in C#?
Answer:
String
: Immutable (cannot be changed after creation).StringBuilder
: Mutable and efficient for string manipulation (e.g., appending, replacing).
15. What is the difference between ref
and out
parameters?
Answer:
ref
: Requires the variable to be initialized before passing it to the method.out
: Does not require initialization before passing it to the method.
16. What is the difference between throw
and throw ex
?
Answer:
throw
: Preserves the original stack trace.throw ex
: Resets the stack trace, making debugging harder.
17. What is the difference between IEnumerable
and ICollection
?
Answer:
IEnumerable
: Provides read-only access to a collection.ICollection
: Provides additional methods for modifying the collection (e.g.,Add
,Remove
).
18. What is the difference between IList
and List
?
Answer:
IList
: An interface that defines methods for a list.List
: A concrete implementation of theIList
interface.
19. What is the difference between HashSet
and List
?
Answer:
HashSet
: A collection of unique elements with fast lookups.List
: A collection of elements that allows duplicates and maintains order.
20. What is the difference between ConcurrentDictionary
and Dictionary
?
Answer:
ConcurrentDictionary
: Thread-safe and designed for concurrent access.Dictionary
: Not thread-safe and should not be used in multi-threaded scenarios.
21. What is the difference between Task.Delay
and Thread.Sleep
?
Answer:
Task.Delay
: Asynchronously pauses the execution without blocking the thread.Thread.Sleep
: Synchronously blocks the thread.
22. What is the difference between ConfigureAwait(true)
and ConfigureAwait(false)
?
Answer:
ConfigureAwait(true)
: Continues on the original context (e.g., UI thread).ConfigureAwait(false)
: Continues on any thread, improving performance in non-UI scenarios.
23. What is the difference between ValueTask
and Task
?
Answer:
ValueTask
: A struct-based alternative toTask
that reduces allocations for synchronous operations.Task
: A class-based type for asynchronous operations.
24. What is the difference between Span<T>
and Memory<T>
?
Answer:
Span<T>
: A stack-only type for representing contiguous memory regions.Memory<T>
: A heap-allocated type for representing memory regions that can be used in asynchronous code.
25. What is the difference between ASP.NET Core
and ASP.NET MVC
?
Answer:
ASP.NET Core
: A modern, cross-platform framework for building web applications and APIs.ASP.NET MVC
: A framework for building web applications using the Model-View-Controller pattern (part of .NET Framework).
These advanced questions and answers should help experienced developers prepare for .NET interviews effectively. Make sure to practice coding examples and understand the underlying concepts thoroughly!
*************** ALL THE BEST *****************
Visit JaganInfo youtube channel for more valuable content https://www.youtube.com/@jaganinfo