Why does IEnumerable.ToList() return List instead of IList?

前端 未结 10 1672
暖寄归人
暖寄归人 2020-12-29 02:23

The extension method ToList() returns a List. Following the same pattern, ToDictionary() returns a Dictionary<

10条回答
  •  孤城傲影
    2020-12-29 02:23

    The short answer is that in general returning the most specific type available is recommended by the authoritative Framework Design Guidelines. (sorry I don't have a citation on hand, but I remember this clearly since it stuck out in contrast to the Java community guidelines which prefer the opposite).

    This makes sense to me. You can always do e.g. IList list = x.ToList(), only the library author needs to be concerned with being able to support the concrete return type.

    ToLookup is the unique one in the crowd. But perfectly within the guidelines: it is the most specific type available that the library authors are willing to support (as others have pointed out, the concrete Lookup type appears to be more of an internal type not meant for public use).

提交回复
热议问题