Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does linq Any() works internally [closed]

Tags:

c#

linq

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

like image 445
Bart Calixto Avatar asked Nov 06 '25 22:11

Bart Calixto


1 Answers

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

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;
}
like image 159
Karl Anderson Avatar answered Nov 08 '25 13:11

Karl Anderson