Monday, June 3, 2013

IEnumerable T Interface

The IEnumerable interface exposes the enumerator, which supports a simple iteration over a collection of a specified type.

Namespace:  
System.Collections.Generic.

While using IEnumerable we provide the type along with the data, represents the type of data which will be stored in the collection.
List 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.

This is a collection where we specify the type of data which will be handled by the Collection.


    List<string> objNames = new List<string>();

    objNames.Add("Name1");
    objNames.Add("Name2");
    objNames.Add("Name3");

    foreach (string strName in objNames)
    {
        Console.WriteLine(objNames);
    }

Notice that unlike the ArrayList example, all the elements in this example have the same type string.

Search Flipkart Products:
Flipkart.com

No comments: