tags:

views:

93

answers:

4

what is IEnumerable in .net?

+2  A: 

First it is an interface. The definition according to MSDN is

Exposes the enumerator, which supports a simple iteration over a non-generic collection.

Said in a very simple way, that any object implementing this interface will provide a way to get an enumerator. An enumerator is used with the foreach as one example.

A List implements the IEnumerable interface.

    // This is a collection that eventually we will use an Enumertor to loop through
    // rather than a typical index number if we used a for loop.
    List<string> dinosaurs = new List<string>();

    dinosaurs.Add("Tyrannosaurus");
    dinosaurs.Add("Amargasaurus");
    dinosaurs.Add("Mamenchisaurus");
    dinosaurs.Add("Deinonychus");
    dinosaurs.Add("Compsognathus");

    Console.WriteLine();

    // HERE is where the Enumerator is gotten from the List<string> object
    foreach(string dinosaur in dinosaurs)
    {
        Console.WriteLine(dinosaur);
    }

    // You could do a 
    for(int i = 0; i < dinosaurs.Count; i++)
    {
        string dinosaur = dinosaurs[i];

        Console.WriteLine(dinosaur);
    }

The foreach looks cleaner.

David Basarab
+4  A: 

It's an interface implemented by Collection types in .NET that provide the Iterator pattern. There also the generic version which is IEnumerable<T>.

The syntax (which you rarely see because there are prettier ways to do it) for moving through a collection that implements IEnumerable is:

IEnumerator enumerator = collection.GetEnumerator();

while(enumerator.MoveNext())
{
    object obj = enumerator.Current;
    // work with the object
}

Which is functionaly equivalent to:

foreach(object obj in collection)
{
    // work with the object
}

If the collection supports indexers, you could also iterate over it with the classic for loop method but the Iterator pattern provides some nice extras like the ability to add synchronization for threading.

Justin Niessner
+1  A: 

The short answer is that it's anything you can use a foreach on.

SWeko
You can foreach over anything with a GetEnumerator method. It doesn't have to support IEnumerable.
Joel Coehoorn
yeah, but most of the time `IEnumerable <==> foreach`
SWeko
+1  A: 

It's ... something... that you can loop over. That might be a List or an Array or (almost) anything else that supports a foreach loop. So that's the first advantage there: if your methods accept an IEnumerable rather than an array or list they become more powerful because you can pass more different kinds of objects to them.

Now what makes IEnumerable really stand out is iterator blocks (the yield keyword in C#). Iterator blocks implement the IEnumerable interface like a List or an Array, but they're very special because unlike a List or Array, they typically only hold a single item in memory at a time. So if you want to loop over the lines in a very large file, for example, you can write an iterator block to handle the file input and you'll never have more than one line of the file at a time. Or if you're reading the results from a large SQL query you can limit your memory use to a single record. Also, this evaluation is lazy, so if you're doing complicated work to evaluate the enumerable as your read from it, that work doesn't happen until it's asked for.

You can think of IEnumerable as if it were a just-in-time List.

Joel Coehoorn
Great definition!
SWeko