(我假设这是LINQ to Objects.其他任何东西都会以不同的方式实现:)
它只是从第一个返回所有内容,然后从第二个返回所有内容.所有数据都是流式传输的.像这样的东西:
public static IEnumerable<T> Concat(this IEnumerable<T> source1,
IEnumerable<T> source2)
{
if (source1 == null)
{
throw new ArgumentNullException("source1");
}
if (source2 == null)
{
throw new ArgumentNullException("source1");
}
return ConcatImpl(source1, source2);
}
private static IEnumerable<T> ConcatImpl(this IEnumerable<T> source1,
IEnumerable<T> source2)
{
foreach (T item in source1)
{
yield return item;
}
foreach (T item in source2)
{
yield return item;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经将它拆分为两个方法,以便可以急切地执行参数验证,但我仍然可以使用迭代器块.(在第一次调用MoveNext()结果之前,迭代器块中的代码不会执行.)