13

I am preparing for my C# EXAM. I am confused about the answer to this question:

A program can use the IEnumerable and IEnumerator interfaces to do which of the following?

a. Use MoveNext and Reset to move through a list of objects.
b. Use foreach to move through a list of objects.
c. Move through a list of objects by index.
d. Use the yield return statement to make a list of objects for iteration.

My answer was b). But the book: MCSD Certification Toolkit says it is a).

Can someone tell me why? I realize that you can obtain the Enumerator using GetEnumerator() and then call the MoveNext and Reset methods to move through the list (and use Current to access the current element referred to by the iterator). But isn't implementing IEnumerable and IEnumerator the reason for an object to be used in a foreach loop?

4
  • 1
    From MSDN: It is a best practice to implement IEnumerable and IEnumerator on your collection classes to enable the foreach (For Each in Visual Basic) syntax, however implementing IEnumerable is not required. Jul 22, 2014 at 15:36
  • 1
    So, you are saying that because the question says both IEnumerable and IEnUMERATOR, it should be a). Thank you very much Hank. You've made my day! Jul 22, 2014 at 15:38
  • 6
    I hate these sorts of questions. They test you on your ability to decipher vague/unclear questions rather than your knowledge on the subject.
    – Phil K
    Jul 22, 2014 at 15:47
  • This answer explains it well. (Although I agree that the question is bad, since both a and b are correct IMO.) Jul 22, 2014 at 15:57

3 Answers 3

4

The correct answer is a and this is clear if you take a look at the definitions of the interfaces:

[GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
[ComVisibleAttribute(true)]
public interface IEnumerable
{
    IEnumerator GetEnumerator();
}

And

[ComVisibleAttribute(true)]
[GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
    Object Current { get; }
    bool MoveNext();
    void Reset();
}

As you notice here, when a type implements the IEnumerable interface, it should have a method that will return a Enumerator, an object that implements the IEnumerator interface.

Then the interface called IEnumerator has one property that holds the current object, when we iterate through a collection and two methods, the MoveNext and the Reset. Under the hood, when we iterate through a collection the Iterator's method called MoveNext is called at first. If this is true we get the first element -- that's the current object. Then method called MoveNext is get called again and again until it returns false. Each time MoveNext is called we get an object from the collection, we iterate through.

Why do we have the Reset method?

As it is stated in MSDN:

Sets the enumerator to its initial position, which is before the first element in the collection.

2
  • But how exactly does this favor a) over b) ? I used to think foreach() needed those interfaces. It does need exactly their functionality. Jul 22, 2014 at 15:45
  • @HenkHolterman I think that a should be favored over b, because foreach uses these methods to do the iteration that foreach does. It refers to the internals. Even we hadn't the foreach() statement, we would have been in position to mimic it using these methods. I agree 100% with you saying that foreach() requires from the collection to implement these interfaces.
    – Christos
    Jul 22, 2014 at 15:51
1

When you implement the interface, you're agreeing to the contract: MoveNext and Reset. The other options for the question refer to concrete classes that implement the interface.

0

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returns false, Current is undefined. To set Current to the first element of the collection again, you can call Reset followed by MoveNext.

click here for detailed explanation

Not the answer you're looking for? Browse other questions tagged or ask your own question.