C# LINQ Contains
last modified November 15, 2026
This article shows how to check whether a collection contains an element with
the LINQ Contains method. We look at value and reference
equality, custom comparers, the difference from Any, and which
Contains method the compiler actually calls.
Enumerable.Contains returns true if a sequence
contains the given element. It compares the elements with the default
equality comparer of the type, or with an IEqualityComparer<T>
that we pass. It goes through the sequence from the start and stops at the
first match.
Contains with simple values
The simplest use of Contains is checking for numbers and strings
in a collection.
int[] numbers = [1, 3, 5, 7, 9, 11, 13];
Console.WriteLine($"Contains 5: {numbers.Contains(5)}");
Console.WriteLine($"Contains 10: {numbers.Contains(10)}");
string[] words = ["sky", "blue", "cloud", "forest", "ocean"];
Console.WriteLine($"Contains 'blue': {words.Contains("blue")}");
Console.WriteLine($"Contains 'red': {words.Contains("red")}");
We check for the existence of numbers and words in arrays.
Console.WriteLine($"Contains 5: {numbers.Contains(5)}");
The method returns true when the value is found and
false otherwise.
$ dotnet run Contains 5: True Contains 10: False Contains 'blue': True Contains 'red': False
Which Contains is called
Several types define their own Contains, and an instance method
or a more specific extension wins over Enumerable.Contains. The
result is the same, but the speed may differ.
| Receiver | Method called | Lookup |
|---|---|---|
T[] (C# 14) |
MemoryExtensions.Contains |
linear, vectorized for primitive types |
T[] (C# 13 and older) |
Enumerable.Contains |
linear |
List<T> |
List<T>.Contains |
linear |
HashSet<T> |
HashSet<T>.Contains |
hash, O(1) |
IEnumerable<T> |
Enumerable.Contains |
delegates to ICollection<T>.Contains when available, otherwise linear |
With the first-class span support of C# 14, arrays convert implicitly to
spans, so numbers.Contains(5) from the previous example compiles
to a call of MemoryExtensions.Contains. We confirmed this by
inspecting the compiled assembly; with <LangVersion>13</LangVersion>
the same code calls Enumerable.Contains.
Case-insensitive search
String comparison in Contains is case-sensitive by default. An
overload takes an IEqualityComparer<T>, such as
StringComparer.OrdinalIgnoreCase.
string[] words = ["Sky", "Blue", "Cloud", "Forest", "Ocean"];
Console.WriteLine(words.Contains("blue"));
Console.WriteLine(words.Contains("blue", StringComparer.OrdinalIgnoreCase));
Console.WriteLine(words.Contains("Fore"));
Console.WriteLine(words.Any(w => w.Contains("Fore")));
We search an array of capitalized words.
Console.WriteLine(words.Contains("blue", StringComparer.OrdinalIgnoreCase));
With the case-insensitive comparer, blue matches
Blue.
Console.WriteLine(words.Contains("Fore"));
Console.WriteLine(words.Any(w => w.Contains("Fore")));
Collection Contains compares whole elements. To find an element
that contains a substring, we combine Any with
string.Contains.
$ dotnet run False True False True
Contains with custom objects
For classes, the default comparer uses reference equality: two distinct
objects with the same data are not equal. Records, and classes that implement
IEquatable<T>, compare by value.
List<User> users =
[
new("John", "Doe", 25),
new("Jane", "Doe", 30)
];
List<Person> people =
[
new("John", "Doe", 25),
new("Jane", "Doe", 30)
];
Console.WriteLine(users.Contains(new User("John", "Doe", 25)));
Console.WriteLine(users.Contains(users[0]));
Console.WriteLine(people.Contains(new Person("John", "Doe", 25)));
Console.WriteLine(people.Contains(new Person("John", "Doe", 26)));
class User(string firstName, string lastName, int age)
{
public string FirstName { get; } = firstName;
public string LastName { get; } = lastName;
public int Age { get; } = age;
}
record Person(string FirstName, string LastName, int Age);
We search a list of class instances and a list of records.
Console.WriteLine(users.Contains(new User("John", "Doe", 25)));
Console.WriteLine(users.Contains(users[0]));
User is a plain class. A new object with the same data is not
found; only the very same instance is.
Console.WriteLine(people.Contains(new Person("John", "Doe", 25)));
Person is a record. The compiler generates
IEquatable<Person>, Equals, and
GetHashCode methods that compare all properties, so an equal
record is found.
$ dotnet run False True True False
Contains with a custom comparer
A custom IEqualityComparer<T> defines equality for one
search, independent of the type's own Equals method.
List<Product> products =
[
new(1, "Laptop", 999.99m),
new(2, "Phone", 699.99m),
new(3, "Tablet", 349.99m),
new(4, "Monitor", 249.99m)
];
var wanted = new Product(0, "PHONE", 0m);
Console.WriteLine(products.Contains(wanted));
Console.WriteLine(products.Contains(wanted, new ProductNameComparer()));
record Product(int Id, string Name, decimal Price);
class ProductNameComparer : IEqualityComparer<Product>
{
public bool Equals(Product? x, Product? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null || y is null) return false;
return string.Equals(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
}
public int GetHashCode(Product obj) =>
StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
}
We look for a product by name only, ignoring case, the ID, and the price.
Console.WriteLine(products.Contains(wanted));
The record equality compares all properties, so the product is not found.
if (ReferenceEquals(x, y)) return true; if (x is null || y is null) return false;
The comparer handles nulls first: two nulls are equal, a null and an object are not.
public int GetHashCode(Product obj) =>
StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
GetHashCode must be consistent with Equals: names
that differ only in case must produce the same hash. The case-insensitive
string comparer provides such a hash.
$ dotnet run False True
Contains in a filter
Contains is often used in a where clause to keep the
elements that appear in another collection.
List<string> fruits = ["apple", "banana", "orange", "mango", "grape"];
string[] wanted = ["banana", "grape", "pear"];
var matching = from fruit in fruits
where wanted.Contains(fruit)
select fruit;
Console.WriteLine(string.Join(", ", matching));
var lookup = new HashSet<string>(wanted);
var missing = fruits.Where(f => !lookup.Contains(f));
Console.WriteLine(string.Join(", ", missing));
We find the fruits that are in the wanted list and those that are not.
where wanted.Contains(fruit)
For each fruit, the clause searches the wanted array.
var lookup = new HashSet<string>(wanted); var missing = fruits.Where(f => !lookup.Contains(f));
Searching an array or a list for every element of another collection takes
O(n*m) time. For larger collections, we put the lookup values into a
HashSet, whose Contains is O(1).
$ dotnet run banana, grape apple, orange, mango
Contains versus Any
Any with a predicate also checks for existence, but it accepts
any condition, not only equality.
List<int> numbers = [1, 3, 5, 7, 9];
Console.WriteLine($"Contains 5: {numbers.Contains(5)}");
Console.WriteLine($"Any equal to 5: {numbers.Any(n => n == 5)}");
Console.WriteLine($"Any even number: {numbers.Any(n => n % 2 == 0)}");
Console.WriteLine($"Any over 8: {numbers.Any(n => n > 8)}");
For a simple equality check, Contains is shorter and can use a
faster lookup. Other conditions need Any.
Console.WriteLine($"Any even number: {numbers.Any(n => n % 2 == 0)}");
No element is even, so Any returns false.
$ dotnet run Contains 5: True Any equal to 5: True Any even number: False Any over 8: True
Contains stops at the first match
On a lazy sequence, Contains consumes only as many elements as it
needs.
IEnumerable<int> Numbers()
{
for (int i = 1; i <= 10; i++)
{
Console.Write($"{i} ");
yield return i;
}
}
bool found = Numbers().Contains(4);
Console.WriteLine($"-> {found}");
bool missing = Numbers().Contains(42);
Console.WriteLine($"-> {missing}");
The iterator prints each number it produces.
bool found = Numbers().Contains(4);
The search ends at the number 4; the remaining numbers are never generated.
bool missing = Numbers().Contains(42);
When the value is missing, the whole sequence is enumerated.
$ dotnet run 1 2 3 4 -> True 1 2 3 4 5 6 7 8 9 10 -> False
Source
First-class Span types (C# 14)
In this article we have checked for the existence of elements in collections
with the LINQ Contains method. See also the
LINQ, LINQ Where, and HashSet
tutorials.
Author
List all C# tutorials.