← All notes

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

Sources

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

Permanent ID: iil:note:49n629k8

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.},
}

Markdown

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.