Check if a value is in an array C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether an array contains a value is a basic operation, but the best method depends on what else you need from the search. In C#, the most common options are Array.Exists, Array.IndexOf, LINQ Contains, and a manual loop. All of them are valid, but they differ in readability, allocation behavior, and how much control they give you.
Use the Simplest API That Matches the Job
If you just want a yes or no answer, LINQ Contains is usually the clearest:
This reads well and is fine for everyday code. For arrays of primitive types, it is also plenty fast for most applications.
If you prefer a framework method that does not read like a query, Array.Exists is another good fit:
Use this pattern when the check is more complex than direct equality.
Get the Index When Position Matters
Sometimes presence is not enough. If you also need the element position, use Array.IndexOf:
This is better than calling Contains and then searching again. One pass is enough.
For custom comparison rules such as case-insensitive matching, a manual loop gives you direct control:
Know When an Array Is the Wrong Data Structure
If you need to perform many repeated membership checks, an array may be the wrong tool. Arrays require linear search, which is O(n) per lookup. A HashSet<T> is usually better for repeated existence checks:
There is a tradeoff: creating the set has an upfront cost. For one lookup, stick with the array. For thousands of lookups, move to a set.
Equality Rules Matter for Reference Types
For arrays of custom objects, "contains" depends on equality semantics. If your type does not implement value equality, two logically equal objects may still compare as different references.
This works because record provides value-based equality. If User were a plain class without equality overrides, the result would be false.
That is why membership checks on object arrays should be reviewed together with the type design, not in isolation.
Prefer Readability Over Cleverness
A manual loop is always available:
This is useful when you need early exit plus extra logic such as logging, counting, or custom comparisons. But if the check is simple, Contains or Array.IndexOf is more readable and easier to review.
Common Pitfalls
- Using
Containsand then performing a second search whenArray.IndexOfwould give both presence and position. - Forgetting that object-array membership depends on equality semantics, not just visible property values.
- Building a
HashSet<T>for a single lookup and paying unnecessary setup cost. - Writing a long manual loop for a simple equality check that
Containsalready expresses clearly. - Assuming array membership checks are cheap even when repeated many times in hot code paths.
Summary
- Use
Containsfor the clearest yes-or-no membership check. - Use
Array.IndexOfwhen you also need the position. - Use
Array.Existsor a manual loop for more complex predicates. - Move to
HashSet<T>when membership checks are frequent and performance matters. - For object arrays, always confirm that equality behavior matches the search you intend.

