我在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?