In simple term collection in C# is a way to store objects. We use System.Collection namespace to work with collection.

IEnumerator

In collection we are able to access the each element using Enumerator. There is an Interface IEnumerator which contains the following method and properties:-


	namespace System.Collections
{
    public interface IEnumerator
    {
	
	//Return the current object 
        object Current { get; }

        
        // move to the next element and return true if not reach at the end poing
        bool MoveNext();
        
        // Reset the collection
        void Reset();
    }
}

		

IEnumerable

There is another interface IEnumerable which contains the single method of type IEnumerator.

 
	namespace System.Collections
{
    
    public interface IEnumerable
    {

        IEnumerator GetEnumerator();
    }
}

		

All the classes which implements this interface IEnumberable, are able to use this method GetEnumerator() and can able to traverse the collection.

For Example

Create an array of type int. Then use GetEnumerator() method and then traverse the data in the following manner:-

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Array ar1 = new int[] { 4, 5, 6,7 };
       
      IEnumerator res = ar1.GetEnumerator();
       
        
        while (res.MoveNext())
        {

            Response.Write(res.Current+"<br/>");
        
        }

    }
}

The output of this code will be as follows:-

IEnumerable in c sharp Figure 1

User defined collection

In this example I simply created a class in which I declared a simple array of size 5 and create indexer for it.

Then I have implemented IEnumerable interface in it and implemented the method GetEnumerator().

Class Code


		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;


public class CustomCollection:IEnumerable
{
    int[] res = new int[5];

    //Inedexer
    public int this[int idx]
    {
        get { return res[idx]; }

        set { res[idx] = value; }
    }

    //implemented method of IEnumerable
    public IEnumerator GetEnumerator()
    {

        foreach (int data in res)
        {
            //This statement is used to Iterate the user defined collection
            yield return data;
        
        }
    
    }
  
}

Calling of Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //create object of custom class
        CustomCollection cc = new CustomCollection();
        // pass value to array through indexer
        cc[0] = 100;
        cc[1] = 200;
        cc[2] = 300;
        cc[3] = 400;
        cc[4] = 500;

        //call GetEnumerator method
        IEnumerator res = cc.GetEnumerator();
        Response.Write("The result of custome class<br/>");
        while (res.MoveNext())
        {

            Response.Write(res.Current + "<br/>");
        }
        
    }
}

	

You will see the following output:-

traversal of IEnumerator Figure 2

Yield Keyword

Yield keyword is used in C# to iterate of type IEnumerable and IEnumerator. It is basically a type of iterator. When yield return statement executed, the current location is remembered by the code and in next iteration execution will be exactly started from where it left last.

For example:-

I have created a simple class in which I have declared method of return type IEnumerator. In this method I have used two yield return statement and then call this method.

Code of Class


public class CustomCollection
{
public IEnumerator Exa_yie()
    {

        yield return "Isha";
        yield return "Malhotra";    
    }
}

Now call this method

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //create object of custom class
        CustomCollection cc = new CustomCollection();
      
        IEnumerator data = cc.Exa_yie();
        while (data.MoveNext())
        {

            Response.Write(data.Current+" ");
        
        }
        
    }
}


Now call this method and checks its output:-

 
ienumerator in c sharp

		Figure 3