为什么IEnumerable< T> .ToList< T>()返回List< T>而不是IList< T>? [英] Why does IEnumerable<T>.ToList<T>() return List<T> instead of IList<T>?

查看:185
本文介绍了为什么IEnumerable< T> .ToList< T>()返回List< T>而不是IList< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展方法 ToList()返回列表< TSource> 。遵循相同的模式, ToDictionary()返回字典< TKey,TSource>

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

我很好奇为什么这些方法不会将其返回值键入为 IList< TSource> IDictionary< TKey,TSource> ; 分别。这似乎更奇怪,因为 ToLookup< TSource,TKey> 将其返回值键入为接口而不是实际实现。

I am curious why those methods do not type their return values as IList<TSource> and IDictionary<TKey, TSource> respectively. This seems even odder because ToLookup<TSource, TKey> types its return value as an interface instead of an actual implementation.

使用 dotPeek 或其他反编译器查看这些扩展方法的来源,我们看到以下实现(显示 ToList()因为它更短):

Looking at the source of those extension methods using dotPeek or other decompiler, we see the following implementation (showing ToList() because it is shorter):

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source) { 
   if (source == null) throw Error.ArgumentNull("source");
   return new List<TSource>(source); 
}

那么为什么这个方法将其返回值键入为接口的特定实现而不是界面本身?唯一的变化是返回类型。

So why does this method type its return value as a specific implementation of the interface and not the interface itself? The only change would be the return type.

我很好奇,因为 IEnumerable<> 扩展非常一致在他们的签名中,除了这两个案例。我一直认为这有点奇怪。

I am curious because the IEnumerable<> extensions are very consistent in their signatures, except for those two cases. I always thought it to be a bit strange.

此外,为了让事情更加令人困惑, ToLookup() 的文档说明:

Additionally, to make things even more confusing, the documentation for ToLookup() states:


根据
指定的键选择器函数从IEnumerable创建一个Lookup。

Creates a Lookup from an IEnumerable according to a specified key selector function.

但是返回类型是 ILookup< TKey,TElement>

Edulinq ,Jon Skeet提到返回类型为列表< T> 而不是 IList< ; T> ,但不会进一步触及主题。

广泛搜索没有答案,所以我在这里问你:

In Edulinq, Jon Skeet mentions that the return type is List<T> instead of IList<T>, but does not touch the subject further.
Extensive searching has yielded no answer, so here I ask you:

是否有任何设计决定背后没有将返回值键入接口,或者它只是偶然发生?

推荐答案

返回列表< T> List< T> 的那些方法不属于 IList< T> 的优点很容易使用。您可以使用 List< T> 执行很多操作,而 IList< T> 。

Returning List<T> has the advantage that those methods of List<T> that are not part of IList<T> are easily used. There are a lot of things you can do with a List<T> that you cannot do with a IList<T>.

相比之下,查询< TKey,TElement> 只有一个可用的方法 ILookup< TKey,TElement> 没有( ApplyResultSelector ),你可能不会最终使用它。

In contrast, Lookup<TKey, TElement> has only one available method that ILookup<TKey, TElement> does not have (ApplyResultSelector), and you probably would not end up using that anyway.

这篇关于为什么IEnumerable&lt; T&gt; .ToList&lt; T&gt;()返回List&lt; T&gt;而不是IList&lt; T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆