C# Array Contains: Methods and Performance
Learn how to check whether a C# array contains a value using LINQ Contains, Array.IndexOf, and manual loops, with performance and edge-case considerations.

Checking whether a C# array contains a specific value is a frequent operation in real-world code. The most direct approach is the LINQ Contains extension method, but the right choice depends on whether you need the index, how you handle case sensitivity, and how often the check runs. This article covers the common methods, their behavior, and the tradeoffs you should consider.
Using LINQ's Contains Method
The simplest way to test for existence is the Contains method from the System.Linq namespace. It works on any type that implements IEnumerable<T>, which includes all single-dimensional arrays. The method returns a bool indicating whether the specified value is present.
using System;
using System.Linq;
int[] numbers = { 1, 2, 3, 4, 5 };
bool hasThree = numbers.Contains(3); // true
bool hasTen = numbers.Contains(10); // falseContains uses the default equality comparer for the element type. For value types like int, this is a simple value comparison. For reference types, it uses the type's Equals implementation. If the class overrides Equals, that custom equality logic is used; otherwise reference equality applies. GetHashCode should be overridden alongside Equals for consistency. This behavior is consistent with LINQ methods that use the default equality comparer and is usually what you expect.
Checking Without LINQ: IndexOf and Manual Loops
If you are not using LINQ or you need the position of the element, Array.IndexOf is a direct alternative. It returns the zero-based index of the first occurrence, or -1 if the value is not found.
int[] numbers = { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(numbers, 3); // 2
bool exists = index >= 0;For a manual loop, you can iterate through the array and return when the element is found. This gives you full control over the comparison logic, which can be useful for custom equality rules.
bool ContainsValue(int[] array, int target)
{
foreach (int value in array)
{
if (value == target)
return true;
}
return false;
}The manual loop is more verbose but avoids LINQ overhead, which can matter in tight loops where the check is called millions of times. However, the difference is usually negligible for typical application code.
Case-Insensitive String Matching
For string arrays, the default Contains performs a case-sensitive ordinal comparison. To ignore case, use the overload that accepts an IEqualityComparer<string>, passing StringComparer.OrdinalIgnoreCase.
using System;
using System.Linq;
string[] fruits = { "Apple", "Banana", "Cherry" };
bool hasBanana = fruits.Contains("banana", StringComparer.OrdinalIgnoreCase); // trueThis overload is part of LINQ and has been available since .NET Framework 3.5, so it works in all modern .NET versions. If you cannot use LINQ, write a manual loop that calls string.Equals with StringComparison.OrdinalIgnoreCase.
Performance and Large Arrays
Both Contains and IndexOf perform a linear scan, so their time complexity is O(n) in the worst case. For a single check on a small array, this is fine. But if you need to perform many existence checks against the same array, consider converting it to a HashSet<T> for O(1) average lookups.
using System;
using System.Collections.Generic;
using System.Linq;
int[] numbers = { 1, 2, 3, 4, 5 };
HashSet<int> set = new HashSet<int>(numbers);
bool hasThree = set.Contains(3); // O(1) averageBuilding the HashSet takes O(n) time and additional memory, but it pays off when you query it repeatedly. For a one-off check, the overhead of building the set is usually not worth it.
Choosing Between Contains and IndexOf
The decision between Contains and IndexOf comes down to whether you need the index. Use Contains when you only care about existence and want the most readable code. Use IndexOf when you need to know where the element is located, for example to replace it or to access adjacent elements. IndexOf also lets you avoid the LINQ dependency if your project does not already use System.Linq.
| Criterion | Contains | IndexOf |
|---|---|---|
| Return value | bool | int (index or -1) |
| Readability | High | Moderate |
| Requires LINQ | Yes | No |
| Custom comparer | Overload available | Overload available in .NET Core |
| Best for | Existence checks | When index is needed |
For most scenarios, Contains is the clearest expression of intent. If you already have a HashSet or Dictionary, their Contains methods are the natural choice.
Multi-Dimensional Arrays and Edge Cases
Contains does not work directly on multi-dimensional arrays because they do not implement IEnumerable<T>. To check a 2D array, you can flatten it with Cast<int>() and then use LINQ.
using System;
using System.Linq;
int[,] grid = { { 1, 2 }, { 3, 4 } };
bool hasFour = grid.Cast<int>().Contains(4); // trueThis works because Cast<int>() iterates over all elements in row-major order. For jagged arrays (arrays of arrays), you need to check each inner array separately or flatten them with SelectMany.
Another edge case is arrays containing null. The default equality comparer treats null as a valid value, so Contains(null) works for reference types. For value types, null is not a valid argument and will cause a compile-time error unless you use a nullable type.
Finally, remember that Contains uses the type's Equals method. If you have a custom class and want to check by a property rather than reference equality, you need to override Equals and GetHashCode, or provide a custom IEqualityComparer<T> to the Contains overload. This is a common source of bugs when developers expect value-based comparison but get reference-based behavior.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.