1. .NET Fundamentals
.NET Ecosystem: Unified platform (formerly .NET Core, .NET 5+) for web, mobile, desktop, cloud, and IoT. Cross-platform, open-source.
CLR (Common Language Runtime): Manages code execution (JIT compilation), memory (GC), and security.
CTS vs CLS:
CTS: Defines data types (e.g.,
int
in C#,Integer
in VB) for cross-language compatibility.CLS: Rules ensuring languages interoperate (e.g., no case sensitivity).
Managed vs Unmanaged Code: Managed runs in CLR; unmanaged (e.g., C++ DLLs) requires manual memory management.
2. Memory Management
Garbage Collection (GC): Automatically reclaims memory.
Generations: Gen 0 (short-lived), Gen 1, Gen 2 (long-lived).
Finalizers: Cleanup before GC, but use
IDisposable
andusing
for deterministic cleanup.
Boxing/Unboxing: Boxing (value → object) incurs overhead; avoid in performance-critical code.
3. OOP Concepts
Value vs Reference Types:
Value:
struct
,enum
(stack or inline in heap).Reference:
class
,interface
,delegate
(heap).
Abstract Class vs Interface:
Abstract: Partial implementation, single inheritance.
Interface: Contract, multiple inheritance.
Polymorphism: Achieved via overriding (
virtual
/override
) or method hiding (new
).
4. Advanced C#
Exception Handling:
try/catch/finally
,throw
. Usethrow ex
to reset stack trace.Async/Await: Non-blocking I/O. Return
Task
(notvoid
) for proper exception handling.LINQ: Deferred execution (e.g.,
IQueryable
) vs immediate (e.g.,ToList()
).
5. ASP.NET Core
Middleware: Pipeline components (e.g., authentication, logging). Order matters.
Dependency Injection: Built-in IoC container. Lifetimes: Transient, Scoped, Singleton.
State Management: Cookies, Sessions, TempData (redirects), ViewBag/ViewData.
JWT Authentication: Token-based security with claims.
6. Entity Framework (ORM)
Code First vs Database First: Code-first defines models in code; DB-first generates models from DB.
Migrations: Manage schema changes.
Eager vs Lazy Loading: Eager loads related data upfront; lazy loads on access (may cause N+1 queries).
7. Design Patterns
Singleton: Ensure one instance (thread-safe with
lock
orLazy<T>
).Factory Method: Decouple object creation (e.g.,
IHttpClientFactory
).Repository: Abstracts data access layer.
8. Testing & Deployment
Unit Testing: xUnit/NUnit with mocking (Moq). Follow Arrange-Act-Assert.
Deployment: Docker containers, Azure App Service, IIS. CI/CD with GitHub Actions/Azure DevOps.
9. Security
Authentication vs Authorization: Who you are vs what you can do.
Data Protection: Encrypt sensitive data using ASP.NET Core Data Protection APIs.
10. Recent Features (.NET 7/8)
Minimal APIs: Simplified syntax for APIs.
Performance: Native AOT,
ref structs
,Span<T>
.Nullable Reference Types: Reduce null-reference exceptions.
Example Interview Q&A
Q: What is the difference between IEnumerable
and IQueryable
?
A:
IEnumerable
: In-memory queries (LINQ to Objects). Executes on the client.IQueryable
: Translates queries to SQL (LINQ to Entities). Executes on the server.
Q: Explain Dependency Injection in ASP.NET Core.
A: Built-in IoC container manages object lifetimes. Register services in Startup.cs
:
services.AddTransient<IService, Service>(); // New instance per request services.AddScoped<IService, Service>(); // Single instance per HTTP request services.AddSingleton<IService, Service>(); // Single instance globally
Q: How does Garbage Collection work?
A: GC reclaims objects no longer referenced. Gen 0 collects frequently (short-lived objects), Gen 2 rarely. Large Object Heap (LOH) for objects >85KB (collected less often).
No comments:
Post a Comment