views:

181

answers:

5

Is there a good resource out there that explains the concept of enumerators and custom enumerators? Particularly one with a good solid example of why you would want to implement IEnumerable yourself and how you would use it effectively?

I occasionally come across yield and I am trying to get a better understanding of it.

+8  A: 

The easiest example:

IEnumerable<string> GetNames()
{
  yield return "Bob";
  yield return "Bob's uncle";
  yield return "Alice";
  yield return "Stacy";
  yield return "Stacy's mom";
}

Usage:

foreach (var name in GetNames())
{
  Console.WriteLine(name);
}

To see it in action, place a debugger breakpoint on every line in the GetNames method.

leppie
That makes sense, but can you explain how the Count() method works? Does it force the enumerator to run through all of the results to count them? Also, why does some code have 'yield break;'?
SLC
`yield break` is basically saying 'stop, no more elements'. And yes, `Count()` (not `Count` or `Length`) will iterate the entire collection if it does not have a shortcut (eg `Count` and `Length`).
leppie
thanks leppie! it's starting to make sense now, but what do you mean by shortcut?
SLC
The underlying type of the `IEnumerable` may be a `List` or an `Array` or a `Hashtable`, all which has a property to determine the length/count without having to traverse the entire collection.
leppie
Ahh, thanks! :)
SLC
`GetNames().ElementAt(4)` has got it goin’ on.
Jeffrey L Whitledge
A: 

A good sample can be found at the MSDN page for IEnumerable.

Andrey
You might want to give an english link :)
leppie
i thought i switched my culture to en-us... Thx!
Andrey
Unfortunately that sample does not cover yield, which is probably the real source of confusion.
Martinho Fernandes
+1  A: 

The best example and reference I found is actually in the C# in Depth book from the almighty Jon Skeet. It's not too costly and it's worth it for everything you'll learn about C#.

Frank
Note that this is chapter 6 in the 1st edition, which used to be (and probably still is) one of the free sample chapters.
Marc Gravell
Have a link: http://www.manning-source.com/books/skeet/Chapter6sample.pdf
Martinho Fernandes
@Martinho: Unfortunately the direct link doesn't work. You need to go via http://manning.com/skeet first - I suspect it's checking referrer. But yes, it's still free :)
Jon Skeet
+3  A: 

Hi,

Another book I found quite useful when I was learning about IEnumerable and IEnumerator is Troelsen's Pro C# 2008 book. It explains what the interfaces contain and how to build iterators with the "yield" keyword.

Hope this help.

Priscilla Tse
+2  A: 

Here are a couple more resources for after you've gotten the basics down.

Wes has a great article on the performance characteristics of iterators:

http://blogs.msdn.com/wesdyer/archive/2007/03/23/all-about-iterators.aspx

If you have questions about why there are so many weird restrictions on what you can do in an iterator block, here's my seven part series on what motivated the unusual rules:

http://blogs.msdn.com/ericlippert/archive/tags/Iterators/default.aspx

Eric Lippert