liveBook · Manning

Yield

As explained in Fabulous Adventures in Data Structures and Algorithms

yield return is C# iterator-block syntax that produces sequence elements on demand during enumeration.

In C#, yield return defines an iterator sequence whose elements are produced on demand. The method body does not execute until enumeration begins, making apparent constant-time construction potentially misleading. section 6.2.4

Recursive permutation generation uses yield return to emit each change incrementally. Enumerating those results still performs the nested work: the example reaches worst-case O(n²) time because each element can trigger up to n nested calls. section 6.2.4

Lazy recursive generation

A recursive Changes method yields an empty sequence at its base case, then yields each permutation formed by inserting the next value. A foreach loop over Changes(4) enumerates the resulting sequence one change at a time.

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains deferred execution, recursive implementation, desugared calls, and enumeration complexity.
Examples
Demonstrates recursive change-ringing permutations and a yield-based InsertAt helper.
What it assumes
Assumes familiarity with C# methods, IEnumerable sequences, foreach enumeration, and recursion.
What you can do after
Apply it via C# iterator methods to produce sequence elements on demand and analyze enumeration costs.
How it connects
Connects iterator sequences with IEnumerable, foreach enumeration, recursion, lazy evaluation, and permutations.

Also explained in

Compare how 10 books explain yield →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.