What is the difference between Contains
and Any
in LINQ?

- 20,585
- 22
- 95
- 108

- 2,898
- 5
- 20
- 28
-
4Not sure why I would get downvoted, perfectly valid question – user2202911 May 07 '14 at 19:37
-
1I think it's been answered here: http://stackoverflow.com/questions/4445219/linq-ring-any-vs-contains-for-huge-collections – msmolcic May 07 '14 at 19:38
-
@msmolcic92 That's a question regarding performance...this is not. – user2202911 May 07 '14 at 19:39
-
1Documentation is at http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx – Tim S. May 07 '14 at 19:39
-
@Joce Stop it with the strawman. I couldn't find anything on Contains & Any, everything I came across on google was regarding LINQ introductions and tutorials. – user2202911 May 07 '14 at 19:45
-
It's all explained right in MS's own documentation. [Any](http://msdn.microsoft.com/en-us/library/bb337697.aspx) vs. [Contains](http://msdn.microsoft.com/en-us/library/bb352880.aspx). – joce May 07 '14 at 19:48
-
2@Joce Here's the thing.. I could not find that page. Hence the question – user2202911 May 08 '14 at 00:08
-
2Have an up-vote for your SEO prowess – sean Jun 30 '15 at 19:55
5 Answers
Contains
takes an object, Any
takes a predicate.
You use Contains
like this:
listOFInts.Contains(1);
and Any
like this:
listOfInts.Any(i => i == 1);
listOfInts.Any(i => i % 2 == 0); // Check if any element is an Even Number
So if you want to check for a specific condition, use Any
. If you want to check for the existence of an element, use Contains
.

- 3,252
- 5
- 38
- 59

- 60,462
- 10
- 96
- 117
-
Technically, Contains takes a TSource, which is either a `Struct` or `Object` depending on how the Collection was defined... – JJS Apr 15 '19 at 15:39
-
1@JJS I meant "object" in the general "everything in C# is an object" sense. Ie, could be an `int`, a `string`, some struct, or a `MyClass`. You are correct of course though – BradleyDotNET Apr 15 '19 at 16:11
Contains
checks if the sequence contains a specified element.
Enumerable.Any
checks if element of a sequence satisfies a condition.
Consider the following example:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
bool contains = list.Contains(1); //true
bool condition = list.Any(r => r > 2 && r < 5);

- 219,104
- 29
- 407
- 436
Contains cares about whether the source collection is an ICollection
, Any does not.
Enumerable.Contains http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#f60bab4c5e27a849
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value)
{
ICollection<TSource> collection = source as ICollection<TSource>;
if (collection != null)
{
return collection.Contains(value);
}
return source.Contains<TSource>(value, null);
}
Enumerable.Any http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#6a1af7c3d17845e3
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource local in source)
{
if (predicate(local))
{
return true;
}
}
return false;
}

- 6,431
- 1
- 54
- 70
-
That's not the *only* difference, but it is one I didn't know about! – BradleyDotNET May 07 '14 at 19:51
Another difference as mentioned here is on the performance
Contains
is O(n) for aList
and O(1) for aHashSet
Any
is simply O(n)

- 31,361
- 18
- 86
- 116
-
1True, but this question is about the extension method `Enumerable.Contains
`, not the interface method `ICollection – Theodor Zoulias Sep 14 '19 at 07:15.Contains`.
Determines whether a sequence contains a specified element by using the default equality comparer.
Determines whether a sequence contains any elements.
As for the documentation:
Can't seem to find to find any documentation on it.
All (most?) LINQ extension methods: here

- 8,488
- 2
- 43
- 93
-
That is one overload of Any (and the one that is less commonly used). I would at least include the more commonly used overload in your answer. – BradleyDotNET May 07 '14 at 19:41
-
My answer already includes **all** overloads ;) (and all (most?) other LINQ extension methods) – RobIII May 07 '14 at 19:41