Dev Tools · 8h ago
C# yield return: The hidden state machine cost
C#'s yield return keyword creates a hidden state machine class, not a simple pause. The compiler transforms the method into an iterator with a state field and switch statement. This allocation can impact performance in hot paths.
Meridian48 take
The article is a solid deep-dive for .NET developers, but the 'hidden cost' is well-known among experienced devs; the real surprise is how elegantly the compiler handles it.
Read the full reporting
The Hidden Cost of yield in C#: What the Compiler Doesn't Tell You →
DEV Community
csharpdotnet