What is the difference between List <string> and IEnumerable <string>

These two seem to be doing the exact things. I timed them - knowing that this is a small example, but they seem to be working at the same speed. Is there any advantage to using one over the other?

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); } 
+4
source share
5 answers

These are different types of objects, with IEnumerable being the interface (not the instance itself, but just a schema for some functionality), and List is the actual class.

There is some kind of “advantage” to using one over the other.

If you are writing a method that you want to be more flexible and only need to list objects, use IEnumerable .

List implements IEnumerable and can be considered as such, 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 the following 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" }; 

All these works:

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

And this is not so:

 OutputList(stringCol); OutputList(stringEn); 
0
source

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

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

The class is responsible for the actual implementation of this functionality.

Your test actually checks for iteration speed through List<string> and string[] . With a small sample size and inner workings of the List<string> class, you really should not see the difference.

+4
source

As for your example, there is no difference how fast the execution and the final result (in particular, only three points).

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

+1
source

Your example really compares List<string> with string[] . Both of these types implement IEnumerable<string> , which is just an interface that defines the behavior of types.

As for the types themselves, the main difference is that the List<string> can be changed as necessary (and for most purposes it does not have a fixed length), and string[] must have the length specified at creation and cannot be changed . The performance of each of them should be belated.

Your example can also be written this way, 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); } 
0
source

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

IEnumerable<string> is an interface that defines a sequence of strings that can be enumerated using a for loop or GetEnumerator() method. It does not contain additional methods that work with its elements, such as a list. In addition, elements may not be stored in memory, but may come from one source, one at a time.

0
source

Source: https://habr.com/ru/post/1411621/


All Articles