IList<T> vs IEnumerable<T>. What is more efficient IList<T> or IEnumerable<T>

Posted by bigb on Stack Overflow See other posts from Stack Overflow or by bigb
Published on 2010-08-21T06:20:40Z Indexed on 2011/01/10 4:54 UTC
Read the original article Hit count: 198

Filed under:
|
|

What is more efficient way to make methods return IList<T> or IEnumerable<T>?

IEnumerable<T> it is immutable collection but IList<T> mutable and contain a lot of useful methods and properties.

To cast IList<T> to IEnumerable<T> it is just reference copy:

IList<T> l = new List<T>();
IEnumerable<T> e = l;

To cast IEnumerable<T> to List<T> we need to iterate each element or to call ToList() method:

IEnumerable<T>.ToList(); 

or may pass IEnumerable<T> to List<T> constructor which doing the same iteration somewhere within its constructor.

List<T> l = new List<T>(e);

Which cases you think is more efficient? Which you prefer more in your practice?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET