Why generic IList<> does not inherit non-generic IList

前端 未结 4 1712
谎友^
谎友^ 2020-12-03 05:44

IList does not inherit IList where IEnumerable inherits IEnumerable.

If out mo

4条回答
  •  青春惊慌失措
    2020-12-03 06:09

    Note that since 2012, in .NET 4.5 and later, there exists a covariant (out modifier) interface,

    public interface IReadOnlyList
    

    see its documentation.

    Usual collection types like List, Collection and YourClass[] do implement IReadOnlyList and because of the covariance can also be used as IReadOnlyList and ultimately IReadOnlyList.

    As you have guessed, you will not be able to modify your list through a IReadOnlyList<> reference.

    With this new interface, you might be able to avoid the non-generic IList all together. However you will still have the problem that IReadOnlyList is not a base interface of IList.

    提交回复
    热议问题