Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing IEnumerable on an Enumerator?

0 views
Skip to first unread message

Gordon Rundle

unread,
Oct 14, 2003, 12:28:18 PM10/14/03
to
It drives me nuts that I can't use foreach with an enumerator
instance. I would like the following to be functionally identical:
foreach (Object o in MyCollection) ...
foreach (Object o in MyCollection.GetEnumerator()) ...

For enumerable types that are under my control, is there any reason
not to create an enumerator type that itself implements IEnumerable?
Like so:

class MyEnumerator : IEnumerable, IEnumerator {
private IEnumerator mEnumerator = null;
public MyEnumerator(IEnumerator e) { mEnumerator = e; }
public MyEnumerator(IEnumerable e) { mEnumerator =
e.GetEnumerator(); }
public object Current { get { return mEnumerator.Current; } }
public void Reset() { mEnumerator.Reset(); }
public bool MoveNext() { return mEnumerator.MoveNext(); }
public IEnumerator GetEnumerator() { return this; }
}


I understand that using the foreach construct, Dispose() will be
called on the enumerator at the end of the loop, if it's defined; what
are the implications for a wrapper class like this? Are there other
possible issues I should be aware of?

thanks,
G. Rundle

Mickey Williams

unread,
Oct 14, 2003, 4:40:25 PM10/14/03
to
"Gordon Rundle" <gordie...@yahoo.com> wrote in message
news:9ace83ee.03101...@posting.google.com...

> It drives me nuts that I can't use foreach with an enumerator
> instance. I would like the following to be functionally identical:
> foreach (Object o in MyCollection) ...
> foreach (Object o in MyCollection.GetEnumerator()) ...
>

The reason is that enumerators are stateful and after the foreach statement
are fully consumed. A type that implements IEnumerable can dispense endless
enumerators.

> I understand that using the foreach construct, Dispose() will be
> called on the enumerator at the end of the loop, if it's defined; what
> are the implications for a wrapper class like this? Are there other
> possible issues I should be aware of?

If the enumerator implements IDisposable it will be disposed. If it doesn't,
it won't.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Mickey Williams

unread,
Oct 14, 2003, 4:47:50 PM10/14/03
to
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:unjoPOpk...@tk2msftngp13.phx.gbl...

> The reason is that enumerators are stateful and after the foreach
statement
> are fully consumed. A type that implements IEnumerable can dispense
endless
> enumerators.

An example may be clearer:

If you allowed enumerators in foreach, this code would probably not work as
expected:

LoanEnumerator loans = borrower.Loans;
foreach(Loan loan in loans)
PrintLoan(loan);
// loans enumerator instance already consumed at this point
foreach(Loan loan in loans)
RecordLoan(loan);

0 new messages