Implementing IEnumerable on an Enumerator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Rundle

    Implementing IEnumerable on an Enumerator?

    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.Ge tEnumerator()) ...

    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(IE numerator e) { mEnumerator = e; }
    public MyEnumerator(IE numerable e) { mEnumerator =
    e.GetEnumerator (); }
    public object Current { get { return mEnumerator.Cur rent; } }
    public void Reset() { mEnumerator.Res et(); }
    public bool MoveNext() { return mEnumerator.Mov eNext(); }
    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

    #2
    Re: Implementing IEnumerable on an Enumerator?

    "Gordon Rundle" <gordie_rundle@ yahoo.com> wrote in message
    news:9ace83ee.0 310140828.79051 3a4@posting.goo gle.com...[color=blue]
    > 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.Ge tEnumerator()) ...
    >[/color]

    The reason is that enumerators are stateful and after the foreach statement
    are fully consumed. A type that implements IEnumerable can dispense endless
    enumerators.
    [color=blue]
    > 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?[/color]

    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





    Comment

    • Mickey Williams

      #3
      Re: Implementing IEnumerable on an Enumerator?

      "Mickey Williams" <my first name at servergeek.com> wrote in message
      news:unjoPOpkDH A.3256@tk2msftn gp13.phx.gbl...
      [color=blue]
      > The reason is that enumerators are stateful and after the foreach[/color]
      statement[color=blue]
      > are fully consumed. A type that implements IEnumerable can dispense[/color]
      endless[color=blue]
      > enumerators.[/color]

      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 );

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



      Comment

      Working...