Custom implementation of interfaces IEnumerable and IEnumerator


Enumeration means iterating through the elements of a collection. The .NET collection objects by default implements the logic so that users can iterate over the elements. there may be occasions when our own custom classes needs to provide some logic to iterate the data members of the classes. Here this logic can be provided by implementing the IEnumerable and IEnumerator interface by ourselves. This article gives an example of the custom implementation of IEnumerable and IEnumerator interfaces.

There are mainly three ways to provide custom implementation of IEnumerable and IEnumerator.

If the class contain a simple data collection, the enumerator of the data can be exposed by using the iterator An iterator automatically handles the the implementation of IEnumerable and IEnumerator The implementation of class MyClass1 is in this way.

If the class does not contain any collection, but contains custom data which may not be in sequence. here also an iterator can be used giving the data one by one The implementation of class MyClass2 is in this way.

The last method to implement is by implementing the IEnumerator directly. This kind of implementation is done for the above two cases,automatically by the compiler.here we manually implements enumeration. The implementation of class MyClass3 is in this way.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

/// Custom Implementation of IEnumerable and IEnumerator
/// For any collection to support the iteration of its
/// members, it must expose an enumerator. using the enumerator
/// we can iterate through the collection.
///
///Implementing IEnumerable provides a IEnumerator, which supports
///iteration over a collection.
///IEnumerable interface contains the method GetEnumerator() which
///returns IEnumerator.
///the definition of IEnumerable is
///
/// public interface IEnumerable
/// {
///
/// IEnumerator GetEnumerator();
/// }
///
///The actual iteration is done by the IEnumerator.
///the IEnumerator contains the members
///property -> object Current -> Gets the current element in the collection.
///method-> bool MoveNext() -> Advances the enumerator to the next element of the collection.
///method -> void Reset() -> Sets the enumerator to its initial position, which is before the first element in the collection.
///the definiton of IEnumerator is
///
/// public interface IEnumerator
/// {
///
/// object Current { get; }
///
///
/// bool MoveNext();
///
/// void Reset();
/// }
///

namespace InterfacesImp
{
class InterfacesImp
{
static void Main(string[] args)
{
MyClass1 mc1 = new MyClass1();
foreach (int i in mc1)
{
Console.WriteLine(i);
}
Console.WriteLine("End of enumeration from class MyClass1\n");

MyClass2 mc2 = new MyClass2();
foreach (int i in mc2.GetValues())
{
Console.WriteLine(i);
}
Console.WriteLine("End of enumeration from class MyClass2\n");

MyClass3 mc3 = new MyClass3();
foreach(int i in mc3)
{
Console.WriteLine(i);
}
Console.WriteLine("End of enumeration from class MyClass3\n");

Console.ReadLine();
}

}


///
/// for providing the enumeration, this class uses the iterator.
/// An iterator automatically handles the the implementation of
/// IEnumerable and IEnumerator
///

class MyClass1 : IEnumerable
{

int[] data = { 1, 2, 3, 4, 5 };

public MyClass1()
{

}


public IEnumerator GetEnumerator()
{
foreach (int i in data)
{
yield return i;

}
}
}

///
/// for providing the enumeraion, this class also uses the iterator.
/// note here that the iterator gives custom data inside a method
/// which return an IEnumerable object
///

class MyClass2
{
public MyClass2()
{
}

public IEnumerable GetValues()
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield return 5;
}
}

///
/// this class provide the GetEnumerator by implementing the IEnumerator directly.
/// this kind of implementation is done for the above two cases,automatically
/// by the compiler.here we manually implements enumeration.
///

class MyClass3:IEnumerable
{
int[] data = {1,2,3,4,5 };

public MyClass3()
{

}

public IEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}

class MyEnumerator : IEnumerator
{
MyClass3 myCollection;
int currentIndex = -1;

internal MyEnumerator(MyClass3 myCollection)
{
this.myCollection = myCollection;
}

public object Current
{
get
{
if (currentIndex == -1)
{
Console.WriteLine("enumeration not started");

}
if (currentIndex == myCollection.data.Length)
{
Console.WriteLine("enumeration end");
}
return myCollection.data[currentIndex];
}
}

public bool MoveNext()
{

currentIndex = currentIndex + 1;
if (currentIndex >= myCollection.data.Length)
return false;
else
return true;

}

public void Reset()
{
currentIndex = -1;
}
}
}


}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: