Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason implementing IEnumerable and IEnumerator [duplicate]

Tags:

c#

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?

like image 696
user3509153 Avatar asked Jul 22 '14 15:07

user3509153


People also ask

What is the use of IEnumerable?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. This works for readonly access to a collection that implements that IEnumerable can be used with a foreach statement. IEnumerator has two methods MoveNext and Reset.

How do IEnumerable and IEnumerator interfaces work in list class?

IEnumerable and IEnumerator interfaces in .NET are implementations of the iterator pattern. So, let’s see how these interfaces work, and how to implement them in our List class here. IEnumerable interface represents an object that can be enumerated, like the List class here. It has one method:

What is the difference between UDC GetEnumerator and IEnumerable?

IEnumerable has GetEnumerator method which must return a class that implements IEnumerator. 3. The UDC.GetEnumerator method must return a class that implements IEnumerator interface (let say a private class MyEnumerator: IEnumerator).

How do I iterate a list in IEnumerable?

IEnumerable interface has only a single method: GetEnumerator, which is used by the clients to enumerate the List. I created another class called ListEnumerator that knows how to iterate the List. It implements a standard interface (IEnumerator) and hides the details of how the List is enumerated.


2 Answers

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.

like image 124
Christos Avatar answered Nov 02 '22 17:11

Christos


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.

like image 31
Big Daddy Avatar answered Nov 02 '22 16:11

Big Daddy