C# Reference ยท LINQ
LINQ Contains() and SequenceEqual() methods
Reference for LINQ Contains() and SequenceEqual() in C#: test membership, compare two sequences element by element and pass custom equality comparers.
Contains() answers "is this value in the sequence?" and SequenceEqual() answers "do these two sequences hold equal elements in the same order?". Both are quantifier-style operators: they run immediately, stop as soon as the answer is known, and return a bool.
Elements are compared with the default equality comparer for the type unless you supply your own IEqualityComparer<T>. That matters for strings (case sensitivity) and for classes (reference versus value equality).
bool has = names.Contains("Ann"); bool same = first.SequenceEqual(second); bool sameIgnoringCase = a.SequenceEqual(b, StringComparer.OrdinalIgnoreCase);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static bool Contains<TSource>( this IEnumerable<TSource> source, TSource value); public static bool Contains<TSource>( this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource>? comparer); public static bool SequenceEqual<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second); public static bool SequenceEqual<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer);
| Name | Description |
|---|---|
source |
The sequence to search. |
value |
The element to look for. |
first, second |
The two sequences to compare. |
comparer |
Decides equality. null or omitted uses the default for the element type. |
Returns: true or false. SequenceEqual() is true only if both sequences have the same length and every pair at the same position is equal.
Errors: ArgumentNullException if a sequence is null.
Usage notes
- Short-circuiting.
Contains()stops at the first match;SequenceEqual()stops at the first difference or when lengths differ. - Collections have their own
Contains(). ForList<T>the instance method is used (a linear scan).HashSet<T>.Contains()is O(1) on average, so build aToHashSet()when you test many values against the same data. Contains()onICollection<T>(arrays,List<T>,HashSet<T>) delegates to the collection's own implementation when no comparer is given.- Order matters in
SequenceEqual(). To compare as multisets, sort both sides first; to compare as sets, useToHashSet()andSetEquals(). - Strings. The default comparison is case-sensitive and ordinal. Pass
StringComparer.OrdinalIgnoreCasefor case-insensitive checks. - Floating point.
NaNequalsNaNunderEquals(), soContains(double.NaN)can succeed even thoughNaN == NaNis false. - Do not use
SequenceEqual()onIQueryableexpecting a server-side comparison; many providers cannot translate it. - Consuming sequences. Both arguments are enumerated, so lazy sources run their pipelines. If either side is a one-shot stream, comparing it consumes it.
- Alternative.
Any(x => x == value)also works but cannot use the collection's optimizedContains().