About interface Enumerable and IEnumerator


T

Tony

Hello!

Interface IEnumerable consist of method GetEnumerator() which will return an
object of type IEnumerator.

Now to my question will every object that GetEnumerator return implement
Current, MoveNext and Reset.
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.

Is this correct understood ?

//Tony
 
Ad

Advertisements

M

Marc Gravell

I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.
If you are writing your own iterator, then yes - they must be implemented
(either explicit or implicit implementation is fine).

However, in C# 2 and above, a lot of this is done for you via "yield
return"; this makes creating iterators very painless.

Marc
 
J

Jon Skeet [C# MVP]

Interface IEnumerable consist of method GetEnumerator() which will return an
object of type IEnumerator.

Now to my question will every object that GetEnumerator return implement
Current, MoveNext and Reset.
Yes, otherwise that wouldn't be implementing IEnumerator.
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.

Is this correct understood ?
It's hard to understand exactly what you mean. If you're *calling*
GetEnumerator() then you're using someone else's implementation to
start with - you wouldn't be implementing them in the first place.

What problem are you trying to solve?

Jon
 
J

Jeroen Mostert

Tony said:
Interface IEnumerable consist of method GetEnumerator() which will return an
object of type IEnumerator.

Now to my question will every object that GetEnumerator return implement
Current, MoveNext and Reset.
Yes, they have to, because that's what implementing IEnumerator means.
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.
Your question seems to be based on a peculiar understanding of how
interfaces work.

If you are implementing GetEnumerator(), you return an object whose class
implements IEnumerator. Now either you are implementing that class yourself,
in which case you must supply definitions, or you are instantiating someone
else's class, in which case the author of that class has already implemented
these methods.

There is no way you can have an IEnumerator that does not implement the
methods required in the interface. An interface is a mandatory contract.

Marc raises the important issue that C# offers support for "automatic"
implementations of IEnumerable and IEnumerator which in many cases obviate
the need for writing code yourself. That is, you can let the compiler create
the necessary classes for you:

public void IEnumerable<int> range(int from, int to) {
for (int i = from; i < to; ++i) {
yield return i;
}
}

Here the compiler will conjure up suitable IEnumerable and IEnumerator
implementations, and no extra code is required.
 
T

Tony

Hello!

In this example I don't have to implement the three method that
GetEnumerator returns. Do you mean that this is the most common situation
that you don't have to implement these three methods?

I didn't fully understood what you meant in the previous answer.

Person[] array =
{
new Person("532513-1234", "Olle", 34),
new Person("123456-4321", "Pelle", 12),
new Person("987654-5678", "Stina", 44)
};

public Form1()
{
IEnumerator iter = array.GetEnumerator();

while (iter.MoveNext())
Console.WriteLine(iter.Current);
...
}

//Tony
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Tony said:
In this example I don't have to implement the three method that
GetEnumerator returns.
You're not *implementing* anything. You're *calling* the methods
implemented by other classes.

What did you expect to have to implement?
Do you mean that this is the most common situation
that you don't have to implement these three methods?
Yes, it's quite rather that you need to implement IEnumerable or
IEnumerator manually yourself - particularly with iterator blocks in C#
2.

I'd also recommend using "foreach" which calls
GetEnumerator()/MoveNext()/Current/Dispose for you behind the scenes.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top