C# – Check if Element is present in List
Use the List<T>.Contains() method when you need to check whether a C# list contains a specific value. The method returns true when a matching element is found and false otherwise.
For simple values such as integers and strings, Contains() is usually the most direct choice. When you need to check an object’s property, match part of a string, use custom conditions, or compare multiple values, methods such as Exists(), LINQ Any(), and All() are more suitable.
C# List.Contains() syntax and return value
To check whether an element is present in a list, call Contains() with the value you want to find.
bool List<int>.Contains(int item)
For a generic List<T>, the general form is:
bool result = list.Contains(item);
If the list contains an element considered equal to item, Contains() returns true. Otherwise, it returns false. The comparison uses the default equality comparer for the list’s element type.
Example 1 – Check if Element is in C# List using Contains()
In the following program, we have a list of integers.
We shall check if element 68 is present in the list or not using Contains() method. As 68 is present in the list, List.Contains() method returns True.
Then we shall check if the element 59 is present in the list. As 59 is not present in the list, List.Contains() method returns false.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//create list
List<int> nums = new List<int>();
nums.Add(52);
nums.Add(68);
nums.Add(73);
//check if element is present in the list
bool isElementPresent = nums.Contains(68);
Console.WriteLine("68 present in the list : "+isElementPresent);
//check if element is present in the list
isElementPresent = nums.Contains(59);
Console.WriteLine("59 present in the list : "+isElementPresent);
}
}
Run the above C# program.
Output
68 present in the list : True
59 present in the list : False
The first call finds 68, so the result is True. The second call does not find 59, so the result is False.
Check if a string exists in a C# List
The same Contains() method works with a List<string>. String matching is based on string equality, so capitalization matters with the default comparison.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> cities = new List<string> {
"Mumbai", "Delhi", "Bengaluru"
};
Console.WriteLine(cities.Contains("Delhi"));
Console.WriteLine(cities.Contains("delhi"));
}
}
True
False
If you need a case-insensitive string check, use a comparison that explicitly specifies the desired StringComparison.
Check a C# List for a string that contains specific text
List<string>.Contains() checks for an entire matching string. It does not test whether one of the list entries contains a substring. For a substring condition, use Exists() or LINQ Any().
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> {
"Ravi Kumar", "Neha Verma", "Sara Khan"
};
bool found = names.Exists(name => name.Contains("Verma"));
Console.WriteLine(found);
}
}
True
Here, the list does not need an element equal to "Verma". The condition succeeds because "Neha Verma" contains that text.
Example 2 – Check if Object is present in the C# List
In this example, we shall check if a given object is present in the list.
For a class that does not define value-based equality, the default equality comparison normally treats two separately created instances as different even when their fields or properties contain the same values. This is why the following newly created Car object is not considered equal to the separate Car instance already stored in the list.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//create list
List<Car> cars = new List<Car>();
//add objects to the list
cars.Add(new Car("Toyota", 1250000));
cars.Add(new Car("Tata", 1300000));
cars.Add(new Car("Honda", 1150000));
//create a car object
Car mycar = new Car("Tata", 1300000);
// check if mycar is present in the list
bool isCarPresent = cars.Contains(mycar);
Console.WriteLine(isCarPresent);
}
}
class Car{
public string name;
public int price;
public Car(string name, int price){
this.name = name;
this.price = price;
}
}
Run the C# program.
Output
False
The values stored in the two Car instances are the same, but they are separate instances and this class does not define value-based equality.
Let us rewrite the above program and modify as shown below.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//create list
List<Car> cars = new List<Car>();
//add objects to the list
Car car1 = new Car("Toyota", 1250000);
cars.Add(car1);
Car car2 = new Car("Tata", 1300000);
cars.Add(car2);
Car car3 = new Car("Honda", 1150000);
cars.Add(car3);
// check if car2 is present in the list
bool isCarPresent = cars.Contains(car2);
Console.WriteLine(isCarPresent);
}
}
class Car{
public string name;
public int price;
public Car(string name, int price){
this.name = name;
this.price = price;
}
}
Run the above C# program.
True
In the above program, the exact car2 instance that was added to the list is passed to Contains(). Therefore, the default equality comparison finds a match and returns True.
Check if a C# List contains an object with a specific property value
When the requirement is to find an object by one of its properties, checking the entire object with Contains() is usually not what you want. Use Exists() with a predicate that describes the property condition.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<Car> cars = new List<Car> {
new Car("Toyota", 1250000),
new Car("Tata", 1300000),
new Car("Honda", 1150000)
};
bool hasTata = cars.Exists(car => car.name == "Tata");
Console.WriteLine(hasTata);
}
}
class Car {
public string name;
public int price;
public Car(string name, int price) {
this.name = name;
this.price = price;
}
}
True
The predicate checks each car’s name field and returns true as soon as a matching object is found.
Use LINQ Any() for property-based List checks in C#
LINQ Any() provides another concise way to test whether at least one element satisfies a condition. Add using System.Linq; before using this extension method.
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> {12, 25, 40, 63};
bool hasNumberAbove50 = numbers.Any(number => number > 50);
Console.WriteLine(hasNumberAbove50);
}
}
True
Use Contains() when you already know the exact value to search for. Use Any() or Exists() when the match depends on a condition.
Check if a C# List contains multiple specified values
If you need to verify that a list contains several required values, LINQ All() can apply Contains() to each required element.
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> {10, 20, 30, 40};
int[] required = {20, 40};
bool containsAll = required.All(number => numbers.Contains(number));
Console.WriteLine(containsAll);
}
}
True
This checks whether every value in required appears in numbers. If even one required value is absent, containsAll becomes false.
Check whether one C# List shares any values with another List
To find whether two lists have at least one value in common, combine Any() with Contains().
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> first = new List<int> {10, 20, 30};
List<int> second = new List<int> {30, 40, 50};
bool hasCommonValue = second.Any(value => first.Contains(value));
Console.WriteLine(hasCommonValue);
}
}
True
The result is True because both lists contain 30.
How C# List.Contains() compares values and objects
List<T>.Contains() uses the default equality comparer for type T. This distinction matters when deciding why a value is or is not found:
- Value types such as
intare compared by their equality semantics. - Strings are compared using string equality; the default comparison is case-sensitive.
- Reference types can define value-based equality by implementing appropriate equality members. If a class does not do so, separate instances normally do not compare equal merely because their fields contain identical values.
- If your search is based on a property or another condition, use
Exists()orAny()instead of expectingContains()to inspect that property automatically.
Choosing Contains(), Exists(), or Any() for a C# List
- Use
Contains(value)to check for one exact value according to the type’s equality rules. - Use
Exists(predicate)to check whether aList<T>contains an element that satisfies a condition. - Use LINQ
Any(predicate)when you want a condition-based check that also works across other LINQ-compatible sequences. - Use
All()together withContains()when every value from another collection must be present.
C# List.Contains() summary
Use List<T>.Contains() when you need a Boolean answer to the question, “Does this list contain this value?” For property checks, substring checks, or other conditions, use Exists() or LINQ Any(). When checking custom objects, remember that the result depends on the equality behavior defined for that type.
In this C# Tutorial, we learned how to check if an element or object is present in the list or not using List.Contains() method.
TutorialKart.com