About interface Enumerable and IEnumerator

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

    About interface Enumerable and IEnumerator

    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


  • Marc Gravell

    #2
    Re: About interface Enumerable and 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.
    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


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: About interface Enumerable and IEnumerator

      On Feb 28, 2:25 pm, "Tony" <johansson.ande rs...@telia.com wrote:
      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

      Comment

      • Tony

        #4
        Re: About interface Enumerable and IEnumerator

        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.GetEnumer ator();

        while (iter.MoveNext( ))
        Console.WriteLi ne(iter.Current );
        ...
        }

        //Tony

        "Marc Gravell" <marc.gravell@g mail.comskrev i meddelandet
        news:O3mUYaheIH A.5296@TK2MSFTN GP05.phx.gbl...
        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
        >
        >

        Comment

        Working...