在查看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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。