Lets Learn

Opinion Matters

Question of the Week # 1

Posted by Ankush on January 24, 2011

Hello Everybody, This is a fresh series of questions I am starting on .NET. Anybody can participate/post questions in this section!!!!

So here goes the first one………Why foreach requires IEnumerable, not IEnumerator in .net framework?

I mean, I know that they are 2 different interfaces and I need to implement IEnumerable in order to get IEnumerator but why not it was implemented to get IEnumerator directly? Any reasons/thoughts/views?

Please post your thoughts!!

Answer: Though Vikas has already summarized it very well (check the comment section) but here is my version of it.

So I asked “why have two interfaces, if the sole purpose of one is to give you the other?”

Here is the answer : so that you can have more than one enumerator going at the same time.

Think of an enumerator as a “bookmark” that you can move forwards through a document. If a collection just implemented IEnumerator, then the collection could only have one bookmark at a time. If there were ten consumers who all wanted to iterate through the collection, they’d have to arrange amongst themselves how to get access to the bookmark without stepping on each other.

Collections that implement IEnumerable are saying “here’s a factory that makes as many bookmarks as you want”. You can get a fresh one any time you want without worrying that someone else’s iteration is going to ruin yours.



5 Responses to “Question of the Week # 1”

  1. Ankush said

    I’ll post the reply on Thursday..so comm..on guys…any thoughts!!!!

    • i had the same question couple days back, and i stumbled upon this explanation on stackoverflow. ->

      n Enumerator shows you the items in a list or collection. Each instance of an Enumerator is a a certain position (the 1st element, the 7th element, etc) and can give you that element (IEnumerator.Current) or move to the next one (IEnumerator.MoveNext). When you write a foreach loop in C#, the compiler generates code that uses an Enumerator.

      An Enumerable is a class that can give you Enumerators. It has a method called GetEnumerator which gives you an Enumerator that looks at its items. When you write a foreach loop in C#, the code that it generates calls GetEnumerator to create the Enumerator used by the loop

      • Ankush said

        Hello Rashmi,

        Thanks for the reply.

        As I said to Vikas in my response, we implement ienumerable only to get the foreach; which returns the Ienumerator object.
        So why C# team decided to design this like that?


  2. Vikas said

    Hi Ankush,
    First of all i would like to thannk for for such a good quality question…pleae keep it up.
    Here is what i think…

    An iterable object (IEnumerable) is some collection of elements, while an iterator (IEnumerator) is like a pointer to the current element in such a collection.

    lets see this with an example

    Imagine a child who is learning how to read. While reading in some book, the child is using one finger as a guide, to keep track how far it has got with reading the current sentence.

    Using that analogy, the sentence would be an object implementing the IEnumerable interface, e.g. IEnumerable. The child’s finger is the iterator / an object implementing the IEnumerator interface, let’s say IEnumerator.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: