This post focuses on practical .NET and Azure API questions that test your ability to design, integrate, and optimize enterprise-grade systems.
1. How can you use LINQ to group a list of numbers and output the number along with its frequency?
Use GroupBy
to group numbers and Select
to project the frequency:
var groupedNumbers = numbers
.GroupBy(n => n)
.Select(g => new { Number = g.Key, Frequency = g.Count() })
.ToList();
This is a classic use of LINQ for aggregation—concise and powerful.
2. How different or similar are dependency injection and dependency inversion? How do you implement them?
- Dependency Inversion Principle (DIP): High-level modules should depend on abstractions.
- Dependency Injection (DI): A design pattern that fulfills DIP by injecting dependencies into classes.
services.AddTransient<IMyService, MyService>();
This promotes loose coupling and testability.
3. How do you manage multiple implementations of an interface in Program.cs?
Use factories or conditional resolution:
services.AddTransient<IMyService, MyService1>();
services.AddTransient<IMyService, MyService2>();
Then resolve based on context or configuration.
4. How do you establish DB connectivity in Entity Framework?
Configure DbContext
with a connection string:
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Use OnConfiguring
for direct setup in smaller apps.
5. What’s the difference between DB First and Code First in EF?
Approach | Description |
---|---|
DB First | Start with an existing database; scaffold models |
Code First | Start with C# classes; EF generates schema |
Use DB First for legacy systems. Use Code First for greenfield development.
6. How do you create a new table using Code First in EF?
Define a model and add a DbSet
:
public class MyNewTable {
public int Id { get; set; }
public string Name { get; set; }
}
public class MyDbContext : DbContext {
public DbSet<MyNewTable> MyNewTables { get; set; }
}
Then run:
dotnet ef migrations add AddMyNewTable
dotnet ef database update
7. What are return types of API controllers and how do they differ?
Return Type | Framework | Description |
---|---|---|
IActionResult |
ASP.NET Core | Flexible, supports multiple response types |
IHttpActionResult |
ASP.NET Web API | Older, used for HTTP response abstraction |
Use IActionResult
for modern APIs—it integrates well with filters and middleware.
8. Is concurrency the same as asynchronous programming?
No. Concurrency is about multiple tasks running simultaneously. Asynchronous programming is about non-blocking operations.
public async Task<IActionResult> GetDataAsync() {
var data = await _service.GetDataAsync();
return Ok(data);
}
Async improves scalability. Concurrency improves throughput.
9. What caching mechanisms have you used?
- In-memory caching: Fast, scoped to app instance.
- Distributed caching: Scalable, e.g., Redis.
- Output caching: For static content.
services.AddMemoryCache();
_cache.Set("key", value, TimeSpan.FromMinutes(5));
10. How do you implement sliding expiration in caching?
var cacheEntryOptions = new MemoryCacheEntryOptions {
SlidingExpiration = TimeSpan.FromMinutes(5)
};
_cache.Set("EmployeeDetails", employeeDetails, cacheEntryOptions);
This resets the expiration timer on each access.
11. What’s the difference between sliding and absolute expiry?
Expiry Type | Behavior |
---|---|
Sliding Expiry | Resets on each access |
Absolute Expiry | Expires after fixed time, regardless of access |
Use sliding for frequently accessed data. Use absolute for predictable cleanup.
12. How can you pass an array of records from EF to the database?
Use AddRange
:
context.MyEntities.AddRange(myEntitiesArray);
context.SaveChanges();
Efficient for bulk inserts.
13. What are defined tables in relation to stored procedures?
Defined tables are explicitly created in the database schema. They’re referenced in stored procedures for input/output operations and table-valued parameters.
14. What’s clean architecture?
Clean architecture separates your app into layers:
- Domain: Core business logic
- Application: Use cases and orchestration
- Infrastructure: DB, APIs, external services
This improves testability, scalability, and maintainability.
15. What’s Eager, Lazy, and Explicit Loading in EF Core?
Loading Type | Behavior | Example |
---|---|---|
Eager | Loads related data immediately | Include(o => o.Customer) |
Lazy | Loads related data on access | public virtual Customer Customer |
Explicit | Loads related data manually | context.Entry(order).Reference(...).Load() |
Choose based on performance and data access patterns.
Top comments (0)