principlecsharpCritical
LINQ .Any VS .Exists - What's the difference?
Viewed 0 times
linqanyexiststhedifferencewhat
Problem
Using LINQ on collections, what is the difference between the following lines of code?
and
When I disassemble
if(!coll.Any(i => i.Value))and
if(!coll.Exists(i => i.Value))When I disassemble
.Exists, it looks like there is no code. Why is that?Solution
List.Exists (Object method - MSDN)
Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.
This has existed since .NET 2.0, so before LINQ. It's meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList).
IEnumerable.Any (Extension method - MSDN)
Determines whether any element of a sequence satisfies a condition.
This is new in .NET 3.5 and uses Func(TSource, bool) as an argument, so this was intended to be used with lambda expressions and LINQ.
In terms of behaviour, these are identical.
Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.
This has existed since .NET 2.0, so before LINQ. It's meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList).
IEnumerable.Any (Extension method - MSDN)
Determines whether any element of a sequence satisfies a condition.
This is new in .NET 3.5 and uses Func(TSource, bool) as an argument, so this was intended to be used with lambda expressions and LINQ.
In terms of behaviour, these are identical.
Context
Stack Overflow Q#879391, score: 498
Revisions (0)
No revisions yet.