C#
arrays
value search
programming tutorial
beginner guide

Check if a value is in an array in C#

Codemia Knowledge Hub ·


For a simple yes or no, use LINQ `Contains`. Use `Array.Exists` when the check is a predicate, `Array.IndexOf` when you also need the position, and a manual loop when you need full control. They differ in readability, allocation behavior and control.

## Contains and Array.Exists for a yes or no

LINQ `Contains` is usually the clearest:

```csharp
using System;
using System.Linq;

int[] numbers = { 10, 20, 30, 40 };

bool hasThirty = numbers.Contains(30);
Console.WriteLine(hasThirty);
```

It reads well and, for arrays of primitive types, is fast enough for most applications.

If you prefer a framework method that does not read like a query, use `Array.Exists`. It suits checks more complex than direct equality:

```csharp
using System;

int[] numbers = { 10, 20, 30, 40 };

bool hasThirty = Array.Exists(numbers, n => n == 30);
Console.WriteLine(hasThirty);
```

## Array.IndexOf when position matters

If you also need the element's position, use `Array.IndexOf`:

```csharp
using System;

string[] names = { "alice", "bob", "charlie" };

int index = Array.IndexOf(names, "bob");

if (index >= 0)
{
    Console.WriteLine($"Found at index {index}");
}
else
{
    Console.WriteLine("Not found");
}
```

One pass gives you both presence and position, so do not call `Contains` and then search again.

## A loop for custom comparisons

For rules such as case-insensitive matching, a manual loop gives direct control:

```csharp
using System;

string[] names = { "Alice", "Bob", "Charlie" };
string target = "alice";
bool found = false;

foreach (string name in names)
{
    if (string.Equals(name, target, StringComparison.OrdinalIgnoreCase))
    {
        found = true;
        break;
    }
}

Console.WriteLine(found);
```

## HashSet<T> for repeated lookups

Searching an array is linear, `O(n)` per lookup. For many repeated membership checks, especially in hot code paths, a `HashSet<T>` is usually better:

```csharp
using System;
using System.Collections.Generic;

int[] source = { 10, 20, 30, 40, 50 };
var set = new HashSet<int>(source);

Console.WriteLine(set.Contains(30));
Console.WriteLine(set.Contains(99));
```

Building the set has an upfront cost. For one lookup, stay with the array. For thousands, move to a set.

## Equality rules for arrays of objects

For custom objects, "contains" depends on equality semantics. Without value equality, two logically equal objects still compare as different references.

```csharp
using System;
using System.Linq;

User[] users =
{
    new User(1, "Alice"),
    new User(2, "Bob")
};

bool hasBob = users.Contains(new User(2, "Bob"));
Console.WriteLine(hasBob);

public record User(int Id, string Name);
```

This works because `record` provides value-based equality. If `User` were a plain class without equality overrides, the result would be `false`. Review membership checks on object arrays together with the type's design.

## When a manual loop is worth it

A plain loop is always available:

```csharp
using System;

int[] numbers = { 1, 2, 3, 4 };
bool found = false;

for (int i = 0; i < numbers.Length; i++)
{
    if (numbers[i] == 3)
    {
        found = true;
        break;
    }
}

Console.WriteLine(found);
```

It helps when you need an early exit plus extra logic such as logging, counting or custom comparisons. For a simple equality check, `Contains` or `Array.IndexOf` is more readable and easier to review.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms