views:

1052

answers:

6

An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"?

This is a core distinction to make, what with LINQ, etc.

Anyway, what is the difference? I can't seem to find a solid definition on the net. Make no mistake, I can find the meaning of the two terms but I get slightly different answers. What would be the best answer for an interview?

IMO an interator "iterates" over a collection, and an enumerator provides the functionality to iterate, but this has to be called.

Also, using the yield keyword is said to save state. What exactly is this state? Is there an example of this benefit occuring?

Thanks

+1  A: 

Enumeration deals with objects while iteration deals with values only. Enumeration is used when we use vector hashtable etc while iteration are used in while loop for loop etc. I've never use the yield keyword so I couldn't tell you.

Lucas McCoy
That may be true but only for dot net. In some languages you can iterate objects/values and enumerate objects/values
Tim Matthews
Well I'm a .NET programmer. Sorry for my ignorance thats just the way I learned how they worked.
Lucas McCoy
+2  A: 

"Iterators are a new feature in C# 2.0. An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or struct without having to implement the entire IEnumerable interface. Instead, you provide just an iterator, which simply traverses the data structures in your class. When the compiler detects your iterator, it will automatically generate the Current, MoveNext and Dispose methods of the IEnumerable or IEnumerable interface." - msdn

John Ellinwood
+6  A: 

Iterating means repeating some steps while enumerating means going throug all values in a collection of values. So enumerating usually requires some form of iteration.

In that way enumerating is a special case of iterating where the step is getting a value from a collection.

Note the usually - enumerating may also be performd recursivly, but recursion and iteration are so closly related that I would not care about this small difference.

And you may also enumerate values you do not explicitly store in a collection. For example you can enumerate the natural number, primes, or what ever but you would calculate this values during the enumeration and not retrieve them from a physical collection. You unterstand this case as enumerating a virtual collection with its values defined by some logic.


I assume Reed Copsey got the point. In C# there are two major ways to enumerate something.

  1. Implement IEnumerable and a class implementing IEnumerator
  2. Implement an iterators with the yield statement

The first way is harder to implement and uses objects for enumerating. The second way is easier to implement and usses continuations.

Daniel Brückner
In C#, though, an iterator is a specific, special construct, as well as just the verb describing the term. An enumerator, as well, is a specific interface. The two have a distinctly different meaning in C#, vs the normal OO terms.
Reed Copsey
Oh yes, you are right. In my terms this is all enumerating and I completly forgot that C# calls this pattern iterator.
Daniel Brückner
+6  A: 

In C# 2+, iterators are a way for the compiler to automatically generate the IEnumerable and/or IEnumerable<T> interfaces for you.

Without iterators, you would need to create a class implementing IEnumerator, including Current, MoveNext, and Reset. This requires a fair amount of work. Normally, you would create a private class that implemtented IEnumerator<T> for your type, then yourClass.GetEnumerator() would construct that private class, and return it.

Iterators are a way for the compiler to automatically generate this for you, using a simple syntax (yield). This lets you implement GetEnumerator() directly in your class, without a second class (The IEnumerator) being specified by you. The construction of that class, with all of its members, is done for you.

Iterators are very developer friendly - things are done in a very efficient way, with much less effort.

When you use foreach, the two will behave identically (provided you write your custom IEnumerator correctly). Iterators just make life much simpler.

Reed Copsey
+3  A: 

To understand iterators we first need to understand enumerators.

Enumerators are specialist objects which provide one with the means to move through an ordered list of items one at a time (the same kind of thing is sometimes called a ‘cursor’). The .NET framework provides two important interfaces relating to enumerators: IEnumerator and IEnumerable. Objects which implement IEnumerator are themselves enumerators; they support the following members:

  • the property Current, which points to a position on the list

  • the method MoveNext, which moves the Current item one along the list

  • the method Reset, which moves the Current item to its initial position (which is before the first item).

On the other hand, Iterаtors implement the enumerаtor pаttern. .NET 2.0 introduced the iterаtor, which is а compiler-mаnifested enumerаtor. When the enumerаble object cаlls GetEnumerаtor, either directly or indirectly, the compiler generаtes аnd returns аn аppropriаte iterаtor object. Optionаlly, the iterаtor cаn be а combined enumerаble аnd enumerаtor object.

The essentiаl ingredient of аn iterаtor block is the yield stаtement. There is one big difference between iterаtors аnd enumerаtors: Iterаtors do not implement the Reset method. Cаlling the Reset method on аn iterаtor cаuses аn exception.

The point of iterators is to allow the easy implementation of enumerators. Where a method needs to return either an enumerator or an enumerable class for an ordered list of items, it is written so as to return each item in its correct order using the ‘yield’ statement.

Joshua
+1  A: 

What C# calls an iterator is more commonly (outside of the C# world) called a generator or generator function (e.g. in Python). A generator function is a specialized case of coroutine. A C# iterator (generator) is a special form of an enumerator (a data type implementing the IEnumerable interface).

I dislike this usage of the term iterator for a C# generator because it is just as much an enumerator as it is an iterator. Too late for Microsoft to change its mind though.

For contrast consider that in C++ an iterator is a value which is used primarily to access sequential elements in a collection. It can be advanced, derferenced to retrieve a value, and tested to see whether the end of the collection has been reached.

cdiggins