Wednesday, June 19, 2013

LINQ 08- IEnumerable

LINQ is designed to work with collections. Common use of collection is iterating through them to read, filter and manipulate them.

Following diagram shows the flow which is commonly used for reading all items with IEnumerable

IEnumerable flow

 

Foreach loop uses the same logic to iterate through items in collections. Behind the scenes .Net  uses IEnumerable for iterating through collections. All the collection classes (from arrays to generic lists) are implemented using IEnumerable. So IEnumerable interface is unavoidable for every .Net programmer.

 

LINQ extends the concept of IEnumerable with extension methods to provide LINQ queries. Even without LINQ, the extension method provides many collection operations like searching, transforming, grouping and ordering using IEnumerable interface.

 

Sample Code

var a = new int[] { 1, 2, 3, 4, 5 };
var e = a.GetEnumerator();
while (e.MoveNext()) {
    Console.WriteLine(e.Current.ToString());
}

No comments:

Post a Comment