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 if an item exists in a C# list collection?
In C#, you can check if an item exists in a List<T> collection using several methods. The most common approach is the Contains() method, which returns true if the item is found and false otherwise.
Syntax
Following is the syntax for the Contains() method −
bool Contains(T item)
Parameters
item − The object to locate in the list. The value can be null for reference types.
Return Value
Returns true if the item is found in the list; otherwise, false.
Using Contains() Method
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> list1 = new List<string>() {
"Lawrence",
"Adams",
"Pitt",
"Tom"
};
Console.WriteLine("List items:");
foreach(string item in list1) {
Console.WriteLine(item);
}
Console.WriteLine("\nChecking if 'Adams' exists...");
if (list1.Contains("Adams")) {
Console.WriteLine("Item exists!");
} else {
Console.WriteLine("Item does not exist!");
}
Console.WriteLine("\nChecking if 'Smith' exists...");
if (list1.Contains("Smith")) {
Console.WriteLine("Item exists!");
} else {
Console.WriteLine("Item does not exist!");
}
}
}
The output of the above code is −
List items: Lawrence Adams Pitt Tom Checking if 'Adams' exists... Item exists! Checking if 'Smith' exists... Item does not exist!
Using Find() Method
The Find() method returns the first element that matches a condition, or the default value if no match is found −
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<int> numbers = new List<int>() { 10, 20, 30, 40, 50 };
int found = numbers.Find(x => x > 25);
if (found != 0) {
Console.WriteLine("Found number greater than 25: " + found);
}
string notFound = new List<string>() { "Apple", "Banana" }.Find(x => x == "Orange");
if (notFound != null) {
Console.WriteLine("Found: " + notFound);
} else {
Console.WriteLine("Orange not found in the list");
}
}
}
The output of the above code is −
Found number greater than 25: 30 Orange not found in the list
Using Any() Method with LINQ
The Any() method from LINQ checks if any element in the list satisfies a given condition −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
public static void Main() {
List<string> fruits = new List<string>() { "Apple", "Banana", "Cherry", "Date" };
bool hasLongName = fruits.Any(fruit => fruit.Length > 5);
Console.WriteLine("Has fruit with name longer than 5 characters: " + hasLongName);
bool hasApple = fruits.Any(fruit => fruit == "Apple");
Console.WriteLine("Contains 'Apple': " + hasApple);
bool hasOrange = fruits.Any(fruit => fruit == "Orange");
Console.WriteLine("Contains 'Orange': " + hasOrange);
}
}
The output of the above code is −
Has fruit with name longer than 5 characters: True Contains 'Apple': True Contains 'Orange': False
Comparison of Methods
| Method | Use Case | Return Type |
|---|---|---|
Contains() |
Check exact match of an item | bool |
Find() |
Get first item that matches a condition |
T (the item type) |
Any() |
Check if any item matches a condition | bool |
Conclusion
The Contains() method is the simplest way to check if a specific item exists in a C# list. For more complex conditions, use Find() to get the actual item or Any() with LINQ to check existence based on custom criteria.
