Dotnet Csharp Interview Questions Scenario Based 2025 Interview Questions & Answers
20 questions available
Mid
Answer
Task: Always allocates; best for long-running async operations.
ValueTask: Avoids allocation when result is synchronous; but dangerous if awaited twice.
IAsyncEnumerable: Async streaming; uses await foreach and yields values over time.
Mid
Answer
.NET uses the Hill Climbing Algorithm:
Measures throughput
Adjusts thread count dynamically
Increases threads until throughput drops
Then finds an optimal stable point
ThreadPool adapts to CPU cores, blocking calls, and workload patterns.
Mid
Answer
They eliminate unnecessary memory allocations:
Work with stack/heap/native memory
No copying of buffers
High-performance parsing, serialization, pipelines
Span cannot escape the stack ? improves safety & performance.
Mid
Answer
Because:
Exceptions bypass the caller and crash the process
Caller cannot await completion
Execution becomes fire-and-forget
Only valid for event handlers.
Mid
Answer
struct ? value type
readonly struct ? immutable + avoids defensive copies
ref struct ? must remain on stack (e.g., Span)
record struct ? value semantics + synthesized equality
Mid
Answer
Generations:
Gen0 ? short-lived objects
Gen1 ? mid-lived
Gen2 ? long-lived (full GC expensive)
LOH:
Objects > 85 KB
Not compacted by default ? fragmentation
.NET 5+ adds optional LOH compaction.
Mid
Answer
When using async/await:
Compiler generates a hidden state machine struct
Stores:
locals
awaiters
continuation context
Executes via MoveNext()
Optimized heavily in .NET 7/8.
Mid
Answer
Common deadlock scenario:
var x = Task.Run(() => DoWorkAsync()).Result;
Because:
UI/ASP.NET sync context waits
Async method attempts to resume on same context
Results in deadlock
Fix:
Use ConfigureAwait(false)
Avoid .Result or .Wait().
Mid
Answer
Because:
External callers may also lock the same reference
Causes global deadlocks
Exposes private synchronization
Use:
private readonly object _lock = new();
Mid
Answer
Singleton ? one instance for entire application
Scoped ? one instance per web request
Transient ? new instance every resolution
DI container creates an object graph and disposes scoped services at request end.
Mid
Answer
DisposeAsync allows async cleanup (DB, IO, network).
Works with:
await using var x = new Resource();
Useful for pooled or high-latency resources.
Mid
Answer
Because:
$"{a}{b}"
creates:
Hidden string builder
Extra allocations
For high-performance logging:
logger.LogInformation("Value {a} {b}", a, b);
Mid
Answer
yield return:
Synchronous iterator
Lazily returns data
IAsyncEnumerable:
Async streaming
Requires await foreach
Ideal for large data sets over network/DB.
Mid
Answer
Reflection:
Scans metadata
Creates objects dynamically
Costly per call
Source Generators:
Compile-time code generation
Zero reflection
Used in System.Text.Json, DI, gRPC.
Mid
Answer
Custom struct overriding Equals() incorrectly:
Ignoring fields
Not overriding GetHashCode()
Mutable fields changing after insert in dictionary.
Mid
Answer
ASP.NET Core does NOT use SynchronizationContext.
It uses TaskScheduler.Default ? no thread affinity.
Prevents deadlocks typical in MVC/WebForms.
Mid
Answer
Uses libuv (older) or managed sockets (newer)
Zero-copy pipeline
I/O polling
Pooled buffers
Single writer per connection
Multiple event loops
Designed for millions of requests.
Mid
Answer
Span-based parsing
No boxing/unboxing
Source-generated serializers
Fewer virtual calls
Less reflection
Less memory churn.
Mid
Answer
IHostedService:
Background process inside ASP.NET Core host
Worker Service:
Template for standalone background apps
Uses generic host
Ideal for schedulers, queues, Kafka, RabbitMQ.
Mid
Answer
Causes:
Too many blocking calls
Long-running sync code on threadpool
Misconfigured MinThreads
Tasks queued faster than executed
Diagnosis:
dotnet-trace
PerfView
Visual Studio Concurrency.