C# List Contains & Exists
last modified July 5, 2023
In this article we show how to check if an element or specific elements are in a list in C#.
C# list is a collection of elements of the same type. The elements can be accessed by index.
The basic two methods that check the existence of an element or elements in a
list are: Contains and Exists.
Alternatively, it is also possible to use Count,
IndexOf, Find, or Any 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.
var words = new List<string> { "falcon", "water", "war", "pen", "ocean" };
var w1 = "war";
var w2 = "cup";
if (words.Contains(w1))
{
Console.WriteLine($"{w1} is in the list");
}
else
{
Console.WriteLine($"{w1} is not in the list");
}
if (words.Contains(w2))
{
Console.WriteLine($"{w2} is in the list");
}
else
{
Console.WriteLine($"{w2} is not in the list");
}
In the example, we check if two words are present in the list of defined words.
$ dotnet run war is in the list cup is not in the list
C# List Exists
The Exists method determines whether a list contains elements that
match the specified predicate.
var words = new List<string> { "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 lambda expression.
if (res)
{
Console.WriteLine("There are words starting in w");
} else
{
Console.WriteLine("There are no words starting in w");
}
A message is printed based on the received value.
$ dotnet run There are words starting in w There are no words ending in c
C# Enumerable.Count
We can check the existence of an element or elements in a list with LINQ's
Count. The Count method returns the number of elements
in a sequence.
There are two overloaded Count methods. One counts all the
elements, the other one counts all the elements that match a condition.
var vals = new List<int> { -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 check if there are negative values in a list with
Count.
int n = vals.Count(e => e < 0);
We use the method that takes a predicate as a parameter.
$ dotnet run There are 2 negative values
C# Enumerable.Any
The Any method determines whether any element of a sequence exists
or satisfies a condition. It returns a boolean true or false.
var vals = new List<int> { -2, 0, -1, 4, 3, 5, 3, 8 };
bool r = vals.Any(e => e < 0);
if (r)
{
Console.WriteLine("There are negative values");
} else
{
Console.WriteLine($"There are no negative values");
}
In the program we check if there are some negative values with Any.
$ dotnet run There are negative values
Source
List class - language reference
In this article we have showed how to check if an element in present in a list in C#.
Author
List all C# tutorials.