Why is List not IEnumerable?

后端 未结 6 860
北海茫月
北海茫月 2021-01-05 02:01

[Edit: My apologies ... the original question wording was ambiguous and I was not getting the responses I am looking for]

For any class X that inherits from class Y,

6条回答
  •  抹茶落季
    2021-01-05 02:21

    Covariance works only for reference types, not for value types. So a List is assignable to an IEnumerable because string is a reference type, but a List is not assignable to an IEnumerable. See section 13.1.3.2 of the C# language specifications for details

    提交回复
    热议问题