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 the elements that match the specified conditions in C#?
To check whether a List contains elements that match specific conditions in C#, you can use the Exists() method. This method takes a predicate (a function that returns true or false) and returns true if at least one element in the list satisfies the condition.
Syntax
Following is the syntax for using List<T>.Exists() method −
public bool Exists(Predicate<T> match)
Parameters
- match − The predicate delegate that defines the conditions to check against the elements.
Return Value
Returns true if the List contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.
Using Exists() with Custom Predicate Method
You can define a separate method that contains your condition logic and pass it to the Exists() method −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsMultipleOfThree(int i) {
return ((i % 3) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(255);
list.Add(315);
list.Add(410);
list.Add(500);
list.Add(600);
list.Add(710);
list.Add(800);
list.Add(1000);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("Does some elements match the predicate = " + list.Exists(IsMultipleOfThree));
}
}
The output of the above code is −
List elements... 255 315 410 500 600 710 800 1000 Does some elements match the predicate = True
Using Exists() with Lambda Expression
A more concise approach is to use lambda expressions directly within the Exists() method −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int> { 15, 22, 35, 48, 51, 63, 77 };
Console.WriteLine("List elements...");
foreach (int num in numbers) {
Console.WriteLine(num);
}
// Check if any number is greater than 50
bool hasLargeNumber = numbers.Exists(x => x > 50);
Console.WriteLine("Contains number > 50: " + hasLargeNumber);
// Check if any number is divisible by 7
bool hasDivisibleBy7 = numbers.Exists(x => x % 7 == 0);
Console.WriteLine("Contains number divisible by 7: " + hasDivisibleBy7);
// Check if any number is negative
bool hasNegative = numbers.Exists(x => x < 0);
Console.WriteLine("Contains negative number: " + hasNegative);
}
}
The output of the above code is −
List elements... 15 22 35 48 51 63 77 Contains number > 50: True Contains number divisible by 7: True Contains negative number: False
Using Exists() with String Lists
The Exists() method works with any type of List, including strings −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };
Console.WriteLine("Names in list:");
foreach (string name in names) {
Console.WriteLine(name);
}
// Check if any name starts with 'C'
bool startsWithC = names.Exists(name => name.StartsWith("C"));
Console.WriteLine("Contains name starting with 'C': " + startsWithC);
// Check if any name has length greater than 5
bool longName = names.Exists(name => name.Length > 5);
Console.WriteLine("Contains name longer than 5 characters: " + longName);
}
}
The output of the above code is −
Names in list: Alice Bob Charlie David Eve Contains name starting with 'C': True Contains name longer than 5 characters: True
Conclusion
The List<T>.Exists() method provides an efficient way to check if any element in a list matches specific conditions. You can use it with custom predicate methods or lambda expressions to define complex matching criteria, making it a versatile tool for list validation in C#.
