ienumerator and ienumerable, functionality of getenumerator

I am trying to understand IEnumerator and IEnumerable. In the code snippet I am inserting below is the example of array list out of msdn. IEnumerator and IEnumerable both are being used in the code snippet.

Here is the question....By definition an interface defines a contract. A class or struct that implements an interface must adhere to its contract. So any method in interface should have no implementation.

Coming back to IEnumerable and IEnumerator in the below code snippet it is being used in the following way

System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();

GetEnumerator method is returning IEnumerator.... How can a method GetEnumerator in an interface IEnumerable have any functionality Methods in an interface are not suppossed to have any functionality right

using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");

// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( "\tCount: {0}", myAL.Count );
Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
Console.Write( "\tValues:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}



Answer this question

ienumerator and ienumerable, functionality of getenumerator

  • Kelly Dyjur

    A method in an interface has no implementation, but a class implementing the interface provides an implementation of that method. Thus a class implementing IEnumerable promises to provide a method GetEnumerator that will return a class implementing IEnumerator:

    public class MyList : IEnumerable {

    public IEnumerator GetEnumerator() {

    return new MyEnumerator();

    }

    }

    public class MyEnumerator : IEnumerator {

    public bool MoveNext() {

    return false;

    }

    public object Current {

    get {

    return null;

    }

    }

    public void Reset() {

    }

    }



  • monsoondawn

    hi Sean,

    thank you for replying to my posting. In the example I have posted on the arraylist the interface is being passed to the method, it is not being derived. Can you explain more about the code snippet you added


  • Ajay

    Actually, in your example, the ArrayList is being passed to the method. If you look at ArrayList, you'll see that it implements IEnumerable. This is what allows it to be accepted by PrintValues. Since ArrayList implements IEnumerable, it can be treated as if it were an IEnumerable (this is known as polymorphism).

    You can never pass an interface to a method, since interfaces never exist independently. To illustrate this, you can't say:

    IEnumerable myEnum = new IEnumerable(); // Compile error

    But you can say:

    IEnumerable myEnum = new ArrayList();

    An interface only "exists" as a contract. Something has to implement that contract to give it force.

    In my snippet I created a MyList class that implemented IEnumerable, and it's GetEnumerator method returned a class called MyEnumerator, which implemented IEnumerator. If I'd tried to return an integer, I would have got a compile error, since int doesn't implement IEnumerator.



  • ienumerator and ienumerable, functionality of getenumerator