Why can't I assign List<T>
to ICollection<I>
where T implements interface I?
For instance:
public interface IObject {}
public class MyObject : IObject {}
public class Stuff
{
ICollection<IObject> Objects {get;set;}
public Stuff()
{
Objects = new List<MyObject>(); //<-- Cannot implicitly convert type 'ICollection<IObject>' to 'List<MyObject>'. An explicit cast exists.
}
}
I realize the solutions to this error are as follows:
Objects = (ICollection<IObject>)new List<MyObject>();
or
Objects = new List<IObject>();
but I am just curious as to why.