linq Any()如何在内部工作 [英] How does linq Any() works internally

查看:78
本文介绍了linq Any()如何在内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道someCollection.Any()在内部如何工作. 我怎么看这个代码?

I'm intrigued to know how someCollection.Any() internally works. how can I see this code ?

推荐答案

所有LINQ方法实际上都是IEnumerable的扩展方法.

All of the LINQ methods are actually extension methods of IEnumerable.

这是Reflector将Any LINQ方法反编译为:

Here is what Reflector decompiles the Any LINQ method to:

public static bool Any<TSource>(this IEnumerable<TSource> source, 
                                Func<TSource, bool> predicate)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    if (predicate == null)
    {
        throw Error.ArgumentNull("predicate");
    }
    foreach (TSource local in source)
    {
        if (predicate(local))
        {
            return true;
        }
    }
    return false;
}

这篇关于linq Any()如何在内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆