Implement Interface IEnumerator and IEnumerable


T

Tony Johansson

Hello!!

Based on the book I read and on my previous question and answers from you it
seems to me that
it's really very rare that I have to implement IEnumerable(one method) and
IEnumerator(three methods) because these are already implemented I think in
every collection class in .Net so I can just use them if I'm not use the
foreach construction instead.

My first question:
So when can it be useful to be obliged to have to Implement these two
interfaces?

Assume for some reason that I must implement these two interfaces.
One method in the IEnumerator namely Current is not type safe because it
return an object
Assume also that I want to have type safe methods so I use generic interface
for IEnumerator<T>

My second question:
Because of method Current is not type safe it seems to be that it's enough
to
use the generic interface for IEnumerator<T> ? which mean I don't have to
use the
generic interface IEnumerable<T> but instead the the IEnumerable

Now to my third question below is an attempt to implement the two interfaces
IEnumerator<T> and IEnumerable in classSomeClass below.
Can you correct me because I will most certain have several errors ?
In this class I must implement four methods these are
MoveNext, Reset and Current for interface IEnumerator<T> and
one method GetEnumerator for interface IEnumerable

class SomeClass<T> : IEnumerable, where T : IEnumerator<T>
{
public bool MoveNext()
{...}

public void Reset()
{...}

public T Current()
{ return T; }

public IEnumerator GetEnumerator()
{ ...}
}

//Tony
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Tony Johansson said:
Based on the book I read and on my previous question and answers from you it
seems to me that
it's really very rare that I have to implement IEnumerable(one method) and
IEnumerator(three methods) because these are already implemented I think in
every collection class in .Net so I can just use them if I'm not use the
foreach construction instead.

*Reasonably* rare, yes. Now that it's so much easier than it was in
C# 1, there are some times when it's worth doing - but it's not
something you do every day.
My first question:
So when can it be useful to be obliged to have to Implement these two
interfaces?

As an example, when I was working on a timetable project, I took a
scheduling request which had a start/end date for possible days. It was
helpful to have a property DateRange (or something similar) which
returned IEnumerable<DateTime>, so I could then do:

foreach (DateTime day in request.DateRange)
{
...
}
Assume for some reason that I must implement these two interfaces.
One method in the IEnumerator namely Current is not type safe because it
return an object
Assume also that I want to have type safe methods so I use generic interface
for IEnumerator<T>
Yes.

My second question:
Because of method Current is not type safe it seems to be that it's enough
to
use the generic interface for IEnumerator<T> ? which mean I don't have to
use the
generic interface IEnumerable<T> but instead the the IEnumerable

Um, your question isn't very clear - but IEnumerable<T> extends
Now to my third question below is an attempt to implement the two interfaces
IEnumerator<T> and IEnumerable in classSomeClass below.
Can you correct me because I will most certain have several errors ?
In this class I must implement four methods these are
MoveNext, Reset and Current for interface IEnumerator<T> and
one method GetEnumerator for interface IEnumerable

class SomeClass<T> : IEnumerable, where T : IEnumerator<T>
{
public bool MoveNext()
{...}

public void Reset()
{...}

public T Current()
{ return T; }

public IEnumerator GetEnumerator()
{ ...}
}

Unless you're writing in C# 1, you almost certainly don't want to
implement IEnumerable<T> by hand yourself. Get the compiler to do it
for you by using iterator blocks.

See http://pobox.com/~skeet/csharp/csharp2/iterators.html

(Or chapter 6 of my book :)
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Tony,

There are a few reasons why you would want a separate implementation of
IEnumerable. You could do it because you want to encapsulate logic for
iterating over items that you have in collections. Now, granted, you can
have a method that will populate a list and return that, but you have the
overhead of the entire list, which you might not need (typically, you
iterate through IEnumerable implementations and that's it).

With .NET 2.0 on (which you are using, since you are using the generic
IEnumerable<T> interface), you don't really have to handle the
implementation yourself, as you can use iterators, which allow you to use
the yield construct to make your implementations foolproof (almost). Check
out the section of the C# Programming Guide titled "Iterators", located at:

http://msdn2.microsoft.com/en-us/library/dscyy5s0.aspx

And here is a link to an MSDN article which will provide some more
insight:

http://msdn.microsoft.com/msdnmag/issues/04/05/C20/default.aspx
 

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