Possible Duplicate:
What's the difference between IEnumerable and Array, IList and List?
What's the difference between the above two?
IEnumerable is a deferred execution while List is an immediate execution. IEnumerable will not execute the query until you enumerate over the data, whereas List will execute the query as soon as it's called. Deferred execution makes IEnumerable faster because it only gets the data when needed.
We aren't forcing the caller to convert their data structure to a List unnecessarily. So it isn't that IEnumerable<T> is more efficient than list in a "performance" or "runtime" aspect. It's that IEnumerable<T> is a more efficient design construct because it's a more specific indication of what your design requires.
IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.
IEnumerable is read-only and List is not. IEnumerable types have a method to get the next item in the collection.
A List<string>
is a concrete implementation of IEnumerable<string>
. The difference is that IEnumerable<string>
is merely a sequence of string
but a List<string>
is indexable by an int
index, can be added to and removed from and have items inserted at a particular index.
Basically, the interface IEnumerable<string>
lets you stream the string
in the sequence but List<string>
lets you do this as well as modify and access the items in the lists in specific ways. An IEnumerable<string>
is general sequence of string
that can be iterated but doesn't allow random access. A List<string>
is a specific random-access variable-size collection.
different.
IEnumerable enables you to iterate through the collection using a for-each loop.
And IEnumerable just have method GetEnumerator.
And List it implement many interface like IEnumerable, Ilist, etc. So many function in List.
In performance IEnumerable faster than List.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With