c# – 为什么在LINQ扩展方法中使用包装器实际的迭代器函数?

在查看Microsoft的各种C#LINQ方法的实现时,我注意到公共扩展方法仅仅是包装器,它以单独的迭代器函数的形式返回实际的实现.

例如(来自System.Linq.Enumerable.cs):

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second) {
    if (first == null) throw Error.ArgumentNull("first");
    if (second == null) throw Error.ArgumentNull("second");
    return ConcatIterator<TSource>(first,second); 
}

static IEnumerable<TSource> ConcatIterator<TSource>(IEnumerable<TSource> first,IEnumerable<TSource> second) { 
    foreach (TSource element in first) yield return element;
    foreach (TSource element in second) yield return element; 
}

包装迭代器的原因是什么,而不是将它们合并为一个并直接返回迭代器?

像这样:

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second) {
    if (first == null) throw Error.ArgumentNull("first");
    if (second == null) throw Error.ArgumentNull("second");
    foreach (TSource element in first) yield return element;
    foreach (TSource element in second) yield return element; 
}

解决方法

Wrappers用于立即检查方法参数(即,当您调用LINQ扩展方法时).否则在您开始使用迭代器之前不会检查参数(即在foreach循环中使用查询调用执行查询的某些扩展方法 – ToList,Count等).此方法用于具有延迟执行类型的所有扩展方法.

如果你将使用没有包装器的方法,那么:

int[] first = { 1,2,3 };
int[] second = null;

var all = first.Concat(second); // note that query is not executed yet
// some other code
...
var name = Console.ReadLine();
Console.WriteLine($"Hello,{name},we have {all.Count()} items!"); // boom! exception here

使用参数检查包装器方法,您将在first.Concat(第二)行获得异常.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...