Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether a List contains a specified element in C#
To check whether a List contains a specified element in C#, you use the Contains() method. This method returns true if the element is found in the list, and false otherwise.
Syntax
Following is the syntax for the Contains() method −
bool Contains(T item)
Parameters
item: The element to search for in the list. The type T represents the element type of the list.
Return Value
Returns a bool value − true if the item is found, false if not found.
Using Contains() with String List
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> countries = new List<string>();
countries.Add("India");
countries.Add("US");
countries.Add("UK");
countries.Add("Canada");
countries.Add("Poland");
countries.Add("Netherlands");
Console.WriteLine("Countries in the list:");
foreach (string country in countries) {
Console.WriteLine(country);
}
Console.WriteLine("\nChecking if specific countries exist:");
Console.WriteLine("Does list contain 'Canada'? " + countries.Contains("Canada"));
Console.WriteLine("Does list contain 'Germany'? " + countries.Contains("Germany"));
}
}
The output of the above code is −
Countries in the list: India US UK Canada Poland Netherlands Checking if specific countries exist: Does list contain 'Canada'? True Does list contain 'Germany'? False
Using Contains() with Integer List
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int>();
numbers.Add(100);
numbers.Add(200);
numbers.Add(300);
numbers.Add(400);
numbers.Add(500);
Console.WriteLine("Numbers in the list:");
foreach (int num in numbers) {
Console.WriteLine(num);
}
Console.WriteLine("\nChecking if specific numbers exist:");
Console.WriteLine("Does list contain 300? " + numbers.Contains(300));
Console.WriteLine("Does list contain 150? " + numbers.Contains(150));
}
}
The output of the above code is −
Numbers in the list: 100 200 300 400 500 Checking if specific numbers exist: Does list contain 300? True Does list contain 150? False
Using Contains() with Custom Objects
When using Contains() with custom objects, the method uses the default equality comparer, which compares object references for reference types.
Example
using System;
using System.Collections.Generic;
class Person {
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public override bool Equals(object obj) {
if (obj is Person other)
return Name == other.Name && Age == other.Age;
return false;
}
public override int GetHashCode() {
return Name.GetHashCode() ^ Age.GetHashCode();
}
}
public class Demo {
public static void Main(String[] args) {
List<Person> people = new List<Person>();
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Bob", 30);
people.Add(person1);
people.Add(person2);
Person searchPerson = new Person("Alice", 25);
Console.WriteLine("Does list contain Alice (25)? " + people.Contains(searchPerson));
Console.WriteLine("Does list contain Charlie (35)? " + people.Contains(new Person("Charlie", 35)));
}
}
The output of the above code is −
Does list contain Alice (25)? True Does list contain Charlie (35)? False
Conclusion
The Contains() method provides an efficient way to check if a List contains a specific element. It returns true if found and false otherwise. For custom objects, you may need to override the Equals() method to define custom equality logic.
