G
Gordon Rundle
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
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