Lowering in C#: What's really going on in your code?
summary
Explains how Roslyn lowers C# constructs into simpler C# and then into IL, using concrete examples: foreach becomes a pattern-based foreach that compiles to a scope + getEnumerator + try/finally + two casts (element and object), interpolated strings initially lower to string.Format (later optimized to concatenation when safe), lambdas generate helper classes and a lambda capture singleton (changed in C#5 to avoid loop-capture bugs), and yield return becomes a state machine with -2 initial state that can wrap inner enumerators, causing hidden allocations and lifetime traps. SharpLab and Roslyn source are used to inspect real output.
AI-written summary · about
chapters (6)
- 0:00Start
- 0:56What is lowering?
- 9:56Why do I want to know?
- 14:50IEnumerable
- 42:15Yield states
- 48:25Captain planet








