I am preparing for my C# EXAM. I am confused about the answer to this question:
A program can use the
IEnumerable
andIEnumerator
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?
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.
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:
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With