Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone please explain this lazy evaluation code?

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?

like image 417
Tejs Avatar asked Apr 29 '10 19:04

Tejs


1 Answers

This has to with:

  • What iEnumerable does when certain methods are called
  • The nature of enumeration and the Yield statement

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.

like image 157
Patrick Karcher Avatar answered Sep 21 '22 17:09

Patrick Karcher