c# - 为什么 Enumerable.Except 可以用于 C# 中的字符串数组?

标签 c# linq extension-methods

示例

这是我在 Pete on Software 上找到的代码示例博客:

var listThree = new string[] { "Pete", "On", "Software" };  
var listFour = new string[] { "Joel", "On", "Software" };  
stringExcept = listThree.Except(listFour);

代码编译并运行。到目前为止一切顺利。

问题

但是,我不明白为什么会这样。

所以,谁能解释为什么我可以使用 Enumerable.Except在字符串数组上?

也许,如果有人可以解释如何读取 Enumerable.Except 的签名并给我一个代码示例,我会很清楚:

public static IEnumerable<TSource> Except<TSource>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second
)

我所知道的

我知道泛型和扩展方法的概念。但是显然还不足以理解上面的代码示例。我也已经使用了一些基本的 Linq 查询。

最佳答案

Except是一种扩展方法,它扩展任何实现 IEnumerable<T> 的类型.这包括 System.Array实现 IEnumerable<T> 的类型.

链接页面上的注释解释了为什么文档不显示 System.Array实现 IEnumerable<T>

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IEnumerable<T> generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

关于c# - 为什么 Enumerable.Except 可以用于 C# 中的字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5057318/

相关文章:

c# - 简单的 LINQ to Entities 翻译

c# - 序列化列表计数而不是列表

c# - 获取 LINQ 列表中的所有子项

c# - 在树状通用列表中按字母数字顺序排列子项的扩展方法

c# - 转换分割字符串时出错

C# 收集两点不返回任何结果

extension-methods - WeakReference的扩展方法

c# - 当同一根标签为 'System.SerializableAttribute' 时,重复 "paste xml as classes"属性

c# - 如何调用IML上的确认对话框?

C#隐藏cmd窗口需要输入