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
First occurrence in the List that matches the specified conditions in C#
To get the first occurrence in a list that matches the specified conditions in C#, you can use the Find() method. This method searches for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire List.
Syntax
Following is the syntax for the Find() −
public T Find(Predicate<T> match)
Parameters
-
match − The
Predicate<T>delegate that defines the conditions to search for.
Return Value
Returns the first element that matches the conditions if found; otherwise, returns the default value for type T (null for reference types, 0 for numeric types).
Using Find() with Delegate Method
You can define a separate method that acts as a predicate and pass it to the Find() method −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsMultipleOfTen(int i) {
return ((i % 10) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(200);
list.Add(215);
list.Add(310);
list.Add(500);
list.Add(600);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("First Occurrence = " + list.Find(IsMultipleOfTen));
}
}
The output of the above code is −
List elements... 200 215 310 500 600 First Occurrence = 200
Using Find() with Lambda Expression
A more concise approach is to use lambda expressions directly in the Find() method −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> list = new List<int>() {255, 315, 410, 500, 600, 710, 800, 1000};
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
// Find first even number
int firstEven = list.Find(x => x % 2 == 0);
Console.WriteLine("First even number = " + firstEven);
// Find first number greater than 500
int firstLarge = list.Find(x => x > 500);
Console.WriteLine("First number > 500 = " + firstLarge);
}
}
The output of the above code is −
List elements... 255 315 410 500 600 710 800 1000 First even number = 410 First number > 500 = 600
Using Find() with Complex Objects
The Find() method works with complex objects as well. Here's an example with a custom class −
using System;
using System.Collections.Generic;
public class Student {
public string Name { get; set; }
public int Age { get; set; }
public Student(string name, int age) {
Name = name;
Age = age;
}
public override string ToString() {
return $"Name: {Name}, Age: {Age}";
}
}
public class Demo {
public static void Main(String[] args) {
List<Student> students = new List<Student>() {
new Student("Alice", 20),
new Student("Bob", 25),
new Student("Charlie", 22),
new Student("Diana", 19)
};
// Find first student over 21
Student result = students.Find(s => s.Age > 21);
if (result != null) {
Console.WriteLine("First student over 21: " + result);
} else {
Console.WriteLine("No student found matching the criteria.");
}
}
}
The output of the above code is −
First student over 21: Name: Bob, Age: 25
Conclusion
The Find() method in C# provides an efficient way to locate the first element in a List that matches specific conditions. It accepts a predicate delegate and returns the first matching element or the default value if no match is found.
