Early Termination

You can extend the functionality of existing yield methods by passing in one or more values or elements that could define a terminating condition within the function by calling a yield break to stop the inner loop from executing.

publicstaticIEnumerable<int>CountUntilAny(intstart,HashSet<int>earlyTerminationSet){intcurr=start;while(true){if(earlyTerminationSet.Contains(curr)){// we've hit one of the ending values
yieldbreak;}yieldreturncurr;if(curr==Int32.MaxValue){// don't overflow if we get all the way to the end; just stop
yieldbreak;}curr++;}}

The above method would iterate from a given start position until one of the values within the earlyTerminationSet was encountered.

// Iterate from a starting point until you encounter any elements defined as
// terminating elements
varterminatingElements=newHashSet<int>{7,9,11};// This will iterate from 1 until one of the terminating elements is encountered (7)
foreach(varxinCountUntilAny(1,terminatingElements)){// This will write out the results from 1 until 7 (which will trigger terminating)
Console.WriteLine(x);}