.NET Core Interview Questions and Answers
1. What is .NET Core?
.NET Core is a free, open-source, cross-platform framework developed by Microsoft for building web, desktop, cloud, mobile, and IoT applications. It runs on Windows, Linux, and macOS and offers high performance and scalability.
2. What is the difference between .NET Core and .NET Framework?
.NET Core is cross-platform, open-source, and modular, while .NET Framework is Windows-only and primarily used for legacy applications.
3. What is CLR?
CLR (Common Language Runtime) is the execution engine of .NET that manages memory, exception handling, security, and garbage collection.
4. What is CoreCLR?
CoreCLR is the cross-platform runtime used by .NET Core and modern .NET applications.
5. What is CTS?
CTS (Common Type System) defines how data types are declared and managed in the .NET runtime.
6. What is CLS?
CLS (Common Language Specification) is a set of rules that ensures interoperability between .NET languages.
7. What is .NET Standard?
.NET Standard is a formal specification of APIs that all .NET implementations must support.
8. What is Kestrel?
Kestrel is the default lightweight, cross-platform web server used in ASP.NET Core applications.
9. What is the .NET CLI?
The .NET CLI provides command-line tools for creating, building, testing, and deploying .NET applications.
10. Explain Program.cs in .NET 6 and later.
Program.cs is the entry point of the application and contains application startup and service configuration logic.
Dependency Injection
11. What is Dependency Injection?
Dependency Injection (DI) is a design pattern that supplies dependencies from an external source rather than creating them inside a class.
12. What are the service lifetimes in .NET Core?
- Transient: New instance every time.
- Scoped: One instance per request.
- Singleton: One instance throughout application lifetime.
13. What is an IoC Container?
An IoC container manages object creation and dependency resolution automatically.
14. Can we use Autofac with .NET Core?
Yes. ASP.NET Core supports third-party DI containers such as Autofac.
15. What is Constructor Injection?
Constructor Injection is the most common DI approach where dependencies are passed through the class constructor.
Middleware
16. What is Middleware?
Middleware components process HTTP requests and responses in ASP.NET Core.
17. Why is middleware order important?
Requests pass through middleware sequentially. Incorrect ordering can cause unexpected behavior.
18. Difference between Use(), Run(), and Map()?
- Use() continues to the next middleware.
- Run() terminates the pipeline.
- Map() branches the pipeline.
19. How do you create custom middleware?
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
}
}
20. What is the request pipeline?
The request pipeline is the sequence of middleware components through which every HTTP request passes.
Configuration
21. What is appsettings.json?
It stores application configuration such as connection strings, logging settings, and API keys.
22. What is IConfiguration?
IConfiguration provides access to configuration values from multiple sources.
23. What are environment-specific configuration files?
Files such as appsettings.Development.json and appsettings.Production.json store environment-specific settings.
24. What is IOptions?
IOptions provides strongly typed access to application configuration values.
25. What are ASP.NET Core environments?
Common environments include Development, Staging, and Production.
ASP.NET Core Web API
26. What is Web API?
Web API is a framework for building HTTP services that can be consumed by web, mobile, and desktop applications.
27. What is Routing?
Routing maps incoming HTTP requests to controller actions.
28. What is Attribute Routing?
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
}
29. What is Model Binding?
Model Binding automatically maps incoming request data to action method parameters.
30. What is Model Validation?
Model Validation ensures submitted data satisfies validation rules.
31. What is IActionResult?
IActionResult provides flexibility to return different HTTP responses.
32. What is ActionResult<T>?
ActionResult<T> combines a specific return type with HTTP response flexibility.
33. What is CORS?
CORS (Cross-Origin Resource Sharing) controls access to resources from different origins.
34. What are Minimal APIs?
var app = WebApplication.Create(args);
app.MapGet("/", () => "Hello World");
app.Run();
35. What is API Versioning?
API Versioning allows multiple versions of an API to coexist.
Entity Framework Core
36. What is Entity Framework Core?
EF Core is an ORM that allows developers to work with databases using .NET objects.
37. What is DbContext?
DbContext represents a session with the database.
38. What is DbSet?
DbSet represents a table within the database.
39. What is Code First?
Code First creates database structures from C# classes.
40. What is Database First?
Database First generates classes from an existing database.
41. What are EF Core Migrations?
dotnet ef migrations add InitialCreate
dotnet ef database update
42. What is Change Tracking?
Change Tracking monitors entity changes and generates appropriate SQL commands.
43. What is Lazy Loading?
Related data is loaded automatically when accessed.
44. What is Eager Loading?
context.Orders.Include(o => o.Customer);
45. What is Explicit Loading?
Related data is loaded manually when required.
Authentication and Authorization
46. What is Authentication?
Authentication verifies a user's identity.
47. What is Authorization?
Authorization determines what actions an authenticated user can perform.
48. What is JWT?
JWT (JSON Web Token) is a secure token format used for authentication.
49. How does JWT Authentication work?
A token is issued after login and sent with each request for validation.
50. What are Claims?
Claims are key-value pairs that contain user information and permissions.
51. What is Role-Based Authorization?
[Authorize(Roles = "Admin")]
52. What is Policy-Based Authorization?
Policy-based authorization provides flexible access control using custom requirements.
Async Programming
53. What is async and await?
They simplify asynchronous programming and improve readability.
54. What is Task?
Task represents an asynchronous operation that does not return a value.
55. What is Task<T>?
Task<T> represents an asynchronous operation that returns a value.
56. What is ConfigureAwait(false)?
It prevents resuming execution on the original synchronization context.
57. What is Task.WhenAll?
Waits for all tasks to complete.
58. What is Task.WhenAny?
Completes when the first task finishes.
Advanced Concepts
59. What is IHttpClientFactory?
IHttpClientFactory manages HttpClient lifetimes and prevents socket exhaustion.
60. What is gRPC?
gRPC is a high-performance RPC framework that uses HTTP/2 and Protocol Buffers.
61. What is Microservices Architecture?
Microservices break an application into independently deployable services.
62. What is a Circuit Breaker Pattern?
The Circuit Breaker Pattern prevents repeated failures when downstream services are unavailable.
63. What is Rate Limiting?
Rate Limiting restricts the number of requests a client can make within a specific time period.
64. What is Health Check Middleware?
Health Checks provide endpoints that verify application and dependency health.
65. What is Native AOT?
Native AOT compiles .NET applications directly to native machine code for faster startup and lower memory usage.
66. What is Source Generation?
Source Generators create C# code at compile time, reducing runtime reflection overhead.
67. What is the Repository Pattern?
The Repository Pattern abstracts data access logic behind interfaces.
68. What is Garbage Collection?
Garbage Collection automatically frees memory occupied by unused objects.
69. What is IDisposable?
IDisposable provides a mechanism for releasing unmanaged resources.
70. Difference between class, struct, and record?
- class: Reference type.
- struct: Value type.
- record: Value-based equality and immutable data support.