So, this question was just asked on SO:
How to handle an "infinite" IEnumerable?
My sample code:
public static void Main(string[] args)
{
foreach (var item in Numbers().Take(10))
Console.WriteLine(item);
Console.ReadKey();
}
public static IEnumerable<int> Numbers()
{
int x = 0;
while (true)
yield return x++;
}
Can someone please explain why this is lazy evaluated? I've looked up this code in Reflector, and I'm more confused than when I began.
Reflector outputs:
public static IEnumerable<int> Numbers()
{
return new <Numbers>d__0(-2);
}
For the numbers method, and looks to have generated a new type for that expression:
[DebuggerHidden]
public <Numbers>d__0(int <>1__state)
{
this.<>1__state = <>1__state;
this.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId;
}
This makes no sense to me. I would have assumed it was an infinite loop until I put that code together and executed it myself.
EDIT: So I understand now that .Take() can tell the foreach that the enumeration has 'ended', when it really hasn't, but shouldn't Numbers() be called in it's entirety before chaining forward to the Take()? The Take result is what is actually being enumerated over, correct? But how is Take executing when Numbers has not fully evaluated?
EDIT2: So is this just a specific compiler trick enforced by the 'yield' keyword?
This has to with:
When you enumerate over any sort of IEnumerable, the class gives you the next item that it's going to give you. It doesn't do something to all its items, it just give you the next item. It decides what that item is going to be. (For example, some collections are ordered, some aren't. Some don't guarantee a particular order, but seem to always give them back in the same order you put them in.).
The IEnumerable extension method Take()
will enumerate 10 times, getting the first 10 items. You could do Take(100000000)
, and it would give you a lot of numbers. But you're just doing Take(10)
. It just asks Numbers()
for the next item . . . 10 times.
Each of those 10 items, Numbers
gives the next item. To understand how, you'll need to read up on the Yield statement. It's syntactic sugar for something more complicated. Yield is very powerful. (I'm a VB developer and am very annoyed that I still don't have it.) It's not a function; it's a keyword with certain restrictions. And it makes defining an enumerator a lot easier than it otherwise might be.
Other IEnumerable extension methods always iterate through every single item. Calling .AsList would blow it up. Using it most LINQ queries would blow it up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With