“YieldExceptionYield” (Solution)


Exception is not going to happen.


Indeed, the line

var numbers = GetSmallNumbers();

is only building a query, but does not execute it. The line

var evenNumbers = numbers.Select(n => n * 2);

is also only building another query without direct execution. Let's look to the last list of the Main method:


This call will get only the first element of the result enumerable (single call of MoveNext() and Current will be executed). Evaluation of the next elements will nor occur. Thus, the code will work without any exceptions.


Last updated