Library

Volume 3 · .NET & C# Deep Dive

Notebook
Sign in

Cheat Sheet — Part 1: .NET & C# Deep Dive

618 words · about 3 min

Key Terms

Term In one line
Transient New instance every time it’s resolved
Scoped One instance per HTTP request (or explicit scope)
Singleton One instance for the application lifetime
Captive dependency A Singleton holding a Scoped service — the classic DI bug; the scoped object never dies
ConfigureAwait(false) Don’t resume on the captured context; use in libraries, unnecessary in ASP.NET Core
Thread pool starvation Blocked threads (.Result, .Wait()) exhaust the pool; throughput collapses under load
IQueryable<T> Builds an expression tree the provider translates to SQL — filtering happens in the database
IEnumerable<T> Operates in memory on already-materialized data
N+1 One query for the parent, then one per child; fix with Include / projection
Record Immutable-by-default reference type with value equality and with expressions
Semantic Kernel Microsoft’s LLM orchestration for .NET: Kernel, Plugins, Planners, Memory

Numbers That Matter

Figure Value Where it bites
IQueryable → IEnumerable too early 5 ms indexed query → 30-second full table scan A single misplaced .ToList() crashes the service under load
DI lifetimes 3 Transient / Scoped / Singleton — know the captive-dependency trap for each pair
CPython-style caching n/a (see Python part)
Middleware order Order is behaviour Exception handler first, auth before authorization, endpoints last

Decision Rules

  • Never inject Scoped into Singleton. The scoped instance is captured for the process lifetime — stale DbContext, leaked connections, cross-request data bleed. If you need it, inject IServiceScopeFactory and create a scope per use.
  • Never block on async. .Result and .Wait() inside an async handler starve the thread pool: each blocked thread is unavailable, requests queue, latency spirals, and it looks like a database problem. Async all the way down, or synchronous all the way down.
  • ConfigureAwait(false) in library code, not in ASP.NET Core app code — there’s no synchronization context to capture there since Core.
  • Keep IQueryable as IQueryable until you mean it. Every .ToList(), .AsEnumerable() or unsupported method call materializes the set and moves the rest of your LINQ into memory.
  • Fix N+1 by projection first, Include second. Projecting to a DTO fetches only what you need; Include fetches whole graphs and can explode into cartesian joins.
  • Use records for DTOs, messages and value objects; use classes when the type has identity and mutable lifecycle.
  • Choose Semantic Kernel when you’re a .NET shop that wants DI-first idioms, native OpenTelemetry, and first-class content-safety filters. Choose LangChain/LangGraph when you need the larger integration ecosystem, Python-native tooling, or explicit graph-based agent control flow.

Say This Out Loud

“IEnumerable<T> runs LINQ in memory on data you’ve already materialized; IQueryable<T> builds an expression tree that EF Core translates into SQL, so the filtering happens in the database. The failure mode is calling .ToList() too early — you pull the whole table into memory and every subsequent Where is a linear scan in your process. I’ve seen that single mistake turn a five-millisecond indexed query into a thirty-second table scan that took the service down under load.”

“Thread pool starvation is my go-to answer for ‘the service got slow but the database looks fine.’ Someone blocked on .Result inside an async handler; each blocked thread is one the pool can’t reuse, requests queue behind it, and the latency graph looks like a resource problem somewhere else entirely. The fix is async all the way down — the diagnosis is thread-pool metrics, not query plans.”

“The DI mistake I check for first is a captive dependency — a Singleton holding something Scoped. It compiles, it passes tests, and in production you get a DbContext that lives forever, holds a connection, and serves one request’s data to another. If a singleton genuinely needs scoped work, it takes an IServiceScopeFactory and creates the scope itself.”