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);
–
–
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.
–
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.
–
–
–
–
–
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.