Why does ICollection implement both IEnumerable and IEnumerable

前端 未结 3 1802
深忆病人
深忆病人 2021-01-13 03:37

Why does ICollection implement both IEnumerable and IEnumerable?

What is the purpose of this? How does

3条回答
  •  野的像风
    2021-01-13 04:19

    IEnumerable itself forces any implementation to also implement the non-generic IEnumerable. This is safe, for the same reasons that IEnumerable is covariant as of .NET 4... you can always convert the T to object for the non-generic form.

    Basically this means that if you've got code which uses a parameter of type IEnumerable, you can still call it with something like List.

    Eric Lippert wrote a blog post recently about why collections end up implementing many interfaces, and Brad Abrams wrote a blog post back in 2005 about the specific IEnumerable/IEnumerable decision.

提交回复
热议问题