Why can I assign List <T> to IEnumerable <T>, but not ICollection <T> or IList <T>

I have 2 types, IFooand Foo, defined as

public interface IFoo
{
    void run()
}

public class Foo : IFoo
{
    public void run() {
        // ...
    }
}

I can appoint List<Foo> IEnumerable<IFoo>, but not ICollection<IFoo>, not IList<IFoo>.

IEnumerable<IFoo> fooEnumerable = new List<Foo>();// works fine
ICollection<IFoo> fooCollection = new List<Foo>();// doesn't work
IList<IFoo>       fooList       = new List<Foo>();// doesn't work

Why? Which allows IEnumerablenot to worry about what it consisted of some type, ICollectionand IListcare?

+4
source share

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


All Articles