44

What is the difference between Contains and Any in LINQ?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user2202911
  • 2,898
  • 5
  • 20
  • 28

5 Answers5

65

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.

MSDN for Contains, Any

dub stylee
  • 3,252
  • 5
  • 38
  • 59
BradleyDotNET
  • 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
16

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);
Habib
  • 219,104
  • 29
  • 407
  • 436
7

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;
}
JJS
  • 6,431
  • 1
  • 54
  • 70
2

Another difference as mentioned here is on the performance

  • Contains is O(n) for a List and O(1) for a HashSet
  • Any is simply O(n)
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • 1
    True, but this question is about the extension method `Enumerable.Contains`, not the interface method `ICollection.Contains`. – Theodor Zoulias Sep 14 '19 at 07:15
0

Contains

Determines whether a sequence contains a specified element by using the default equality comparer.

Any

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

RobIII
  • 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