Sunday, June 2, 2013

IEnumerable Interface

The IEnumerable interface exposes the enumerator, which supports a simple iteration over a non-generic collection.

Namespace:  System.Collections
ArrayList is a built in class that implements IEnumerable, any class which implements IEnumerable interface should define the GetEnumerator method of the IEnumerable  interface, hence the items in the class can be queried using a foreach loop as follows.

    ArrayList objArrList = new ArrayList();
    //
    objArrList.Add("Name1");
    objArrList.Add("Name2");
    objArrList.Add("Name3");
    objArrList.Add(5);
    objArrList.Add(6);
    //
    foreach (object objItem in objArrList)
    {
        Console.WriteLine(objItem);
    }

This is a generic collection where we do not specify anything about the type of the data which will be handled by the Collection. Notice that the first 3 items in the ArrayList are strings while the last 2 items are integers.

Search Flipkart Products:
Flipkart.com

No comments: