相关疑难解决方法(0)

为什么Single()不会在找到多个元素时直接返回?

我在Enumerable.Single方法中发现(大致)这个代码,同时用一些反编译器检查它:

foreach (TSource current in source)
{
    if (predicate(current))
    {
        result = current;
        num += 1L;
    }
}

if (num > 1L)
{
     throw Error.MoreThanOneMatch();
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它会在投掷之前循环遍历所有项目.它为什么不打破num > 1

.net c# linq internals

18
推荐指数
1
解决办法
366
查看次数

标签 统计

.net ×1

c# ×1

internals ×1

linq ×1