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
Find a specific element in a C# List
In C#, you can find specific elements in a List<T> using various built-in methods. The most common approach is using the Find() method with a lambda expression to specify search criteria.
Syntax
Following is the syntax for using the Find() method −
T element = list.Find(predicate);
Where predicate is a lambda expression that returns true for the element you want to find.
Using Find() Method
The Find() method returns the first element that matches the specified condition. If no element is found, it returns the default value for the type (0 for int, null for reference types).
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> myList = new List<int>() {
5, 10, 17, 19, 23, 33
};
Console.WriteLine("List:");
foreach(int i in myList) {
Console.WriteLine(i);
}
int val = myList.Find(item => item % 2 == 0);
Console.WriteLine("First even number: " + val);
int largeVal = myList.Find(item => item > 20);
Console.WriteLine("First number greater than 20: " + largeVal);
}
}
The output of the above code is −
List: 5 10 17 19 23 33 First even number: 10 First number greater than 20: 23
Using FindAll() Method
To find all elements that match a condition, use the FindAll() method, which returns a new list containing all matching elements −
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> numbers = new List<int>() {
2, 8, 15, 22, 27, 30, 45
};
List<int> evenNumbers = numbers.FindAll(x => x % 2 == 0);
Console.WriteLine("Even numbers:");
foreach(int num in evenNumbers) {
Console.WriteLine(num);
}
List<int> numbersAbove20 = numbers.FindAll(x => x > 20);
Console.WriteLine("\nNumbers greater than 20:");
foreach(int num in numbersAbove20) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Even numbers: 2 8 22 30 Numbers greater than 20: 22 27 30 45
Using Contains() Method
To check if a list contains a specific value, use the Contains() method, which returns true or false −
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> fruits = new List<string>() {
"Apple", "Banana", "Cherry", "Date"
};
Console.WriteLine("Fruits list: " + string.Join(", ", fruits));
bool hasApple = fruits.Contains("Apple");
bool hasGrape = fruits.Contains("Grape");
Console.WriteLine("Contains Apple: " + hasApple);
Console.WriteLine("Contains Grape: " + hasGrape);
}
}
The output of the above code is −
Fruits list: Apple, Banana, Cherry, Date Contains Apple: True Contains Grape: False
Comparison of Search Methods
| Method | Return Type | Purpose |
|---|---|---|
| Find() | T (single element) | Returns the first element matching the condition |
| FindAll() | List<T> | Returns all elements matching the condition |
| Contains() | bool | Checks if a specific value exists in the list |
Conclusion
C# provides multiple methods to find elements in a List: Find() for the first match, FindAll() for all matches, and Contains() to check existence. These methods use lambda expressions or direct value comparison to locate specific elements efficiently.
