ZetCode

C# List Contains & Exists

last modified October 16, 2026

In this article we show how to check if an element or specific elements are in a list in C#.

A C# List<T> is a collection of elements of the same type. The elements can be accessed by index.

The two basic methods that check the existence of an element or elements in a list are Contains and Exists. Alternatively, we can use the IndexOf method or the LINQ Any and Count methods.

C# List Contains

The Contains method checks if an element is present in the list.

public bool Contains(T item);

The method returns a boolean value.

Program.cs
List<string> words = ["falcon", "water", "war", "pen", "ocean"];

string[] tests = ["war", "cup"];

foreach (var w in tests)
{
    if (words.Contains(w))
    {
        Console.WriteLine($"{w} is in the list");
    }
    else
    {
        Console.WriteLine($"{w} is not in the list");
    }
}

In the example, we check if two words are present in the list of words.

$ dotnet run
war is in the list
cup is not in the list

C# Contains and equality

The Contains method compares the elements with the Equals method. Records compare their values, while classes compare references by default.

Program.cs
List<UserRecord> records = [new("John Doe", 34), new("Roger Roe", 47)];
List<UserClass> objects = [new("John Doe", 34), new("Roger Roe", 47)];

Console.WriteLine(records.Contains(new UserRecord("John Doe", 34)));
Console.WriteLine(objects.Contains(new UserClass("John Doe", 34)));

var john = objects[0];
Console.WriteLine(objects.Contains(john));

record UserRecord(string Name, int Age);

class UserClass(string name, int age)
{
    public string Name => name;
    public int Age => age;
}

A new record with the same values is found in the list; a new object of a class with the same values is not. The class would have to override Equals and GetHashCode.

$ dotnet run
True
False
True

C# List Exists

The Exists method determines whether a list contains elements that match the specified predicate.

Program.cs
List<string> words = ["falcon", "water", "war", "pen", "ocean"];

bool res = words.Exists(e => e.StartsWith('w'));

if (res)
{
    Console.WriteLine("There are words starting in w");
}
else
{
    Console.WriteLine("There are no words starting in w");
}

bool res2 = words.Exists(e => e.EndsWith('c'));

if (res2)
{
    Console.WriteLine("There are words ending in c");
}
else
{
    Console.WriteLine("There are no words ending in c");
}

In the program, we check if there are some words starting in 'w' and ending in 'c'.

bool res = words.Exists(e => e.StartsWith('w'));

The Exists method takes a predicate function as a parameter. The predicate is in the form of a lambda expression.

$ dotnet run
There are words starting in w
There are no words ending in c

C# case-insensitive search

Contains on a list of strings is case-sensitive. To ignore the case, we use Exists with a string comparison, or the LINQ Contains overload which takes an equality comparer.

Program.cs
List<string> words = ["Falcon", "Water", "War", "Pen", "Ocean"];

Console.WriteLine(words.Contains("war"));
Console.WriteLine(words.Exists(e => e.Equals("war", StringComparison.OrdinalIgnoreCase)));
Console.WriteLine(words.Contains("war", StringComparer.OrdinalIgnoreCase));

The first check fails, because "war" and "War" differ in case.

$ dotnet run
False
True
True

C# List IndexOf

The IndexOf method returns the index of the first occurrence of an element, or -1 if the element is not found. The FindIndex method does the same for the first element that matches a predicate.

Program.cs
List<string> words = ["falcon", "water", "war", "pen", "ocean", "war"];

Console.WriteLine(words.IndexOf("war"));
Console.WriteLine(words.LastIndexOf("war"));
Console.WriteLine(words.IndexOf("cup"));
Console.WriteLine(words.FindIndex(e => e.Length == 3));

Besides knowing whether an element exists, we also learn its position.

$ dotnet run
2
5
-1
2

C# Enumerable.Any

The LINQ Any method determines whether any element of a sequence exists or satisfies a condition. Unlike Exists, which is a method of List<T>, Any works with any sequence, such as arrays, sets, or query results.

Program.cs
List<int> vals = [-2, 0, -1, 4, 3, 5, 3, 8];

if (vals.Any(e => e < 0))
{
    Console.WriteLine("There are negative values");
}
else
{
    Console.WriteLine("There are no negative values");
}

Console.WriteLine(vals.Any());
Console.WriteLine(new List<int>().Any());

In the program, we check if there are some negative values with Any. Without a predicate, Any checks whether the sequence has any elements.

$ dotnet run
There are negative values
True
False

C# Enumerable.Count

The LINQ Count method with a predicate returns the number of elements that match a condition.

Program.cs
List<int> vals = [-2, 0, -1, 4, 3, 5, 3, 8];

int n = vals.Count(e => e < 0);

if (n == 0)
{
    Console.WriteLine("There are no negative values");
}
else
{
    Console.WriteLine($"There are {n} negative values");
}

In the program, we count the negative values in a list. If we only need to know whether such an element exists, Any or Exists is better, because they stop at the first match, while Count goes through the whole list.

$ dotnet run
There are 2 negative values

Source

List<T> class

In this article we have shown how to check if an element is present in a list in C#.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.