IEnumerable<T> to List<T>

c#, c#-4.0

Solution

Not all `IEnumerable<T>` are `List<T>`. The reverse is true.

You can either try to cast to `List<T>` which is bad practice and could fail if it really is not a list or you can create a new list from the enumeration

new List<T>(yourEnumerable);

or using Linq

yourEnumerable.ToList();

Problem

Ok I have looked all around and can't find an answer. I have a method that returns an ``` IEnumerable<ICar> ``` and the calling method is storing the results of the method in ``` List<ICar> ``` but I get the following error. ``` System.Collections.Generic.IEnumerable<Test.Interfaces.ICar> to System.Collections.Generic.List<Test.Interfaces.ICar>. An explicit conversion exists (are you missing a cast?) ``` I looked on msdn at ``` IEnumerable<T> interface and List<T> class. ``` The following line is from msdn. ``` public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable ``` I just don't understand why I can't assign ``` IEnumerable<ICar> to List<ICar>. ``` Can someone please explain this to me. What am I missing.

Original source