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?