✅ .NET Core Interview Questions with Examples (Easy Explanations)

 

🔹 1. General .NET Core Basics

Q1. What is .NET Core?
👉 Answer:
.NET Core is like the engine of your application. It lets you build apps that run on Windows, Linux, and Mac.

Example:

dotnet new console -o MyApp cd MyApp dotnet run

This creates and runs a simple console app.


Q2. Difference between .NET Framework and .NET Core?
👉 Answer:

  • .NET Framework: Windows-only (old car 🚗 that only runs on one type of road).

  • .NET Core: Cross-platform, modern, faster (new car 🏎 that runs anywhere).


Q3. What is Middleware in ASP.NET Core?
👉 Answer:
Middleware is like checkpoints in a toll road 🚦. Each one checks/does something before passing the request.

Example:

app.Use(async (context, next) => { Console.WriteLine("Before request"); await next.Invoke(); Console.WriteLine("After request"); });

Here the middleware logs before and after handling a request.


Q4. What is Kestrel?
👉 Answer:
Kestrel is the built-in web server for .NET Core.
Think of it as the engine that runs your website before IIS/NGINX serves it to the world.


🔹 2. C# Language Features

Q5. What are Records in C# 9?
👉 Answer:
Records are like data containers – they store values and are immutable (cannot be changed once created).

Example:

public record Person(string Name, int Age); var p1 = new Person("Alex", 30); Console.WriteLine(p1.Name); // Output: Alex

Q6. Difference between Task and ValueTask?
👉 Answer:

  • Task = always creates an object (extra memory).

  • ValueTask = avoids extra memory if the result is already available.

Example:

public async Task<int> GetDataAsync() => 10; public async ValueTask<int> GetDataFastAsync() => 10;

Q7. What is Pattern Matching?
👉 Answer:
Pattern matching helps check types in a simpler way.

Example:

object obj = "Hello"; if (obj is string s) { Console.WriteLine(s.ToUpper()); // Output: HELLO }

🔹 3. Dependency Injection

Q8. What is DI (Dependency Injection)?
👉 Answer:
Instead of a class creating its own dependencies, they are given from outside. This makes testing and maintenance easier.

Without DI:

public class Car { private Engine _engine = new Engine(); // tightly coupled }

With DI:

public class Car { private Engine _engine; public Car(Engine engine) // injected via constructor { _engine = engine; } }

In .NET Core:

services.AddScoped<Engine>(); services.AddScoped<Car>();

Q9. What’s the difference between AddTransient, AddScoped, AddSingleton?
👉 Answer:

  • Transient – new object every time.

  • Scoped – one object per request.

  • Singleton – one object for the whole app.

Example:
Imagine coffee cups:

  • Transient = new cup for every sip ☕

  • Scoped = one cup per customer 👤

  • Singleton = one thermos for the whole café 🏪


🔹 4. Entity Framework Core

Q10. What is AsNoTracking()?
👉 Answer:
When fetching data from DB, EF tracks changes by default.
If you don’t need tracking (read-only), use AsNoTracking() for faster performance.

Example:

var users = context.Users.AsNoTracking().ToList();

Q11. What is a Migration in EF Core?
👉 Answer:
Migration = a version update for the database when your model changes.

Example:

dotnet ef migrations add AddEmailToUser dotnet ef database update

This adds a new column (Email) to the User table.


🔹 5. ASP.NET Core Features

Q12. What is Routing?
👉 Answer:
Routing decides which function handles which URL.

Example:

[HttpGet("api/hello")] public string Hello() => "Hello World!";

Now calling http://localhost/api/hello → returns "Hello World!".


Q13. What is a Minimal API (from .NET 6)?
👉 Answer:
A simpler way to build APIs without controllers.

Example:

var app = WebApplication.CreateBuilder(args).Build(); app.MapGet("/hello", () => "Hello World"); app.Run();

Q14. What is SignalR?
👉 Answer:
SignalR is used for real-time communication (like WhatsApp chat).

Example:

  • User A sends a message → instantly pushed to User B.


🔹 6. Security

Q15. What is JWT Authentication?
👉 Answer:
JWT (JSON Web Token) = digital ticket 🎟 that proves identity.
Instead of saving session on server, JWT is sent with every request.

Example JWT looks like:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Q16. What is CORS?
👉 Answer:
CORS = allows or blocks cross-domain requests.
For example: if your API is on api.com but frontend is on myapp.com.

Code Example:

app.UseCors(builder => builder.WithOrigins("https://myapp.com") .AllowAnyMethod() .AllowAnyHeader());

That’s the style:

  • Simple definition

  • Analogy (real-life example)

  • Code snippet

Comments

Popular posts from this blog

.NET Core Interview Questions and Answers for 10+ Years Experienced Professionals

What are SOLID Principles?

.NET Core Senior Interview Q&A