C# yield iterators defer work until enumeration
Summary
A C# iterator produces elements through yield return. Calling the iterator method does not immediately execute its body; enumeration drives the work. [S1, S2]
Details
After a consumer receives an element, the iterator can resume after the previous yield. yield break ends the sequence early, and reaching the end of the body also ends it. Repeating enumeration runs the iterator again, so its work and side effects may repeat. The interface IEnumerable<T> alone does not tell a caller whether a sequence is already stored or generated lazily. [S1, S2]
Claims
- id: C1 claim: "yield return supplies the next element." source: S1 evidence: "to provide the next value in iteration"
- id: C2 claim: "yield break ends iteration explicitly." source: S1 evidence: "to explicitly signal the end of iteration"
- id: C3 claim: "The iterator body starts when enumeration begins." source: S2 evidence: "Calling an iterator method doesn't run its body immediately"
Sources
- [S1] yield statement. Microsoft and .NET documentation contributors. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield. CC-BY-4.0; license: https://raw.githubusercontent.com/dotnet/docs/main/LICENSE. Accessed 2026-11-05.
- [S2] Iterators. Microsoft and .NET documentation contributors. https://learn.microsoft.com/en-us/dotnet/csharp/iterators. CC-BY-4.0; license: https://raw.githubusercontent.com/dotnet/docs/main/LICENSE. Accessed 2026-11-05.
Attribution and notices
This note is CC BY 4.0. Source material has been condensed and restated; short evidence excerpts retain their source license.
Microsoft .NET documentation text is CC BY 4.0. This is an original condensed explanation. No source code is reproduced.
License: CC-BY-4.0 · Original synthesis by iinventedlight, drafted by Librarian with AI assistance; based on the attributed sources
Cite this
Librarian. n.d.. "C# yield iterators defer work until enumeration." iinventedlight Library, iil:note:49n629k8. https://iinventedlight.com/library/notes/csharp-iterators-yield/. License: CC-BY-4.0.
@misc{iil_note_49n629k8,
author = {{Librarian}},
title = {{C\# yield iterators defer work until enumeration}},
howpublished = {iinventedlight Library},
url = {https://iinventedlight.com/library/notes/csharp-iterators-yield/},
note = {Permanent ID iil:note:49n629k8. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-iterators-yield.md.