Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
List<string> alpha = new List<string>(new string[] { "a", "b", "c" });
foreach (var letter in alpha)
    Console.WriteLine(letter);
IEnumerable<string> _alpha = new[] {"a", "b", "c"};
foreach(var _letter in _alpha)
     Console.WriteLine(_letter);
                In the second example, you've actually created an array. Because IEnumerable is an interface, you cannot create an object of this type. Arrays in .NET simply implement the IEnumerable interface. So does List.
– Cody Gray
                May 9, 2012 at 19:54
                "What is the different between List<T> and IEnumerable<T>"? Well, try adding a new item to a variable of type IEnumerable<T>. You can't, because IEnumerable<T> represents the ability to enumerate the contents of a class. List<T> is one such class, but it can also do other things (like adding and removing items.)
– dlev
                May 9, 2012 at 20:03

IEnumerable<string> is an interface. List<string> is a class that implements that interface.

An interface simply defines functionality that a class that implements that interface must implement.

The class is responsible for actually implementing that functionality.

Your test is actually testing the speed of iterating through a List<string> and a string[]. With the small size of the sample and the inner workings of the List<string> class, you really shouldn't see a difference.

Actually, I think there is no difference because also List uses an array behind the covers. – Tudor May 9, 2012 at 19:57

They are different type of objects, with IEnumerable being an interface (not instance itself, just a schema for some functionality) while List is actual class.

There is sort of an "advantage" to using one over the other.

If you are writing a method which you want to be more flexible, and only needs to enumerate through the objects, then use IEnumerable.

List implements IEnumerable, and can be treated as such, just like Collection, and many other classes.

Take this for example:

public void OutputEnumerable(IEnumerable<string> strings)
    foreach (string s in strings)
        Console.WriteLine(s);
public void OutputList(List<string> strings)
    foreach (string s in strings)
        Console.WriteLine(s);

Then you have this data:

List<string> stringList = new List<string>(new [] { "a", "b", "c" });
Collection<string> stringCol = new Collection<string>(new [] { "a", "b", "c" });
IEnumerable<string> stringEn = new [] { "a", "b", "c" };

These all work:

OutputEnumerable(stringList);
OutputEnumerable(stringCol);
OutputEnumerable(stringEn);
OutputList(stringList);

And these do not:

OutputList(stringCol);
OutputList(stringEn);

In regards to your example, there is no real difference as far as execution speed and the end result (in particular with only three items).

IEnumerable<T> is an interface, List<T> is an actual class that implements this interface.

The second example creates an array whereas the first creates a list and an array. Possibly optimized if compiling as release, otherwise the second one is probably faster (if doing a many times) – Oskar Kjellin May 9, 2012 at 19:55 @OskarKjellin - A micro optimization at best. "Probably" is somethign that should be backed up by measurements. – Oded May 9, 2012 at 19:56 If the compiler isn't smart enough to optimize out the creation of that temporary array, then I give up... – Cody Gray May 9, 2012 at 19:57 Okay, the first one takes 374 ms and the second one 240 ms, doing 1000 times in release mode. So the first one is slower as I guessed – Oskar Kjellin May 9, 2012 at 20:01 @OskarKjellin - Averaged out over how many executions? And what are the results if you reverse the iterations (IEnumerable<T> before List<T>? – Oded May 9, 2012 at 20:02

Your example is really comparing List<string> to string[]. Both of these types implement IEnumerable<string>, which is just an interface that specifies the behaviour of the types.

With regards to the types themselves, the main difference is that List<string> can be resized as necessary (and for most purposes doesn't have a fixed length), while string[] must have its length specified upon creation and cannot be resized. The performance of each should be an afterthought.

Your example could also be written like this, with exactly the same meaning:

IEnumerable<string> alpha = new List<string>(new string[] { "a", "b", "c" });
foreach (var letter in alpha)
    Console.WriteLine(letter);
IEnumerable<string> _alpha = new[] {"a", "b", "c"};
foreach(var _letter in _alpha)
    Console.WriteLine(_letter);

List<string> is a concrete implementation of an element collection that has various methods such as Add, Remove, Sort, etc. It has all the elements already in memory and can access them randomly through an indexer.

IEnumerable<string> is an interface defining a sequence of strings that can be enumerated using a for loop or the GetEnumerator() method. It does not contain additional methods that operate on its elements like the list. Also, the elements may not be stored in memory but come from some source one at a time.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.