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
C# Program to Show the Use of Exists Property
The Exists method in C# is a useful method that checks whether any element in a List<T> matches a given condition. This method takes a predicate as an argument and returns a bool value indicating whether any element exists in the list that satisfies the specified condition.
Syntax
Following is the syntax for the Exists method
public bool Exists(Predicate<T> match)
Parameters
match A
Predicate<T>delegate that defines the conditions of the elements to search for.
Return Value
Returns true if at least one element matches the specified predicate; otherwise, false.
Using Exists to Check for Specific Values
Example
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<string> fruits = new List<string>() { "apple", "banana", "orange", "grape", "mango" };
bool exists = fruits.Exists(f => f.Equals("apple"));
if (exists) {
Console.WriteLine("Apple exists in the list");
} else {
Console.WriteLine("Apple does not exist in the list");
}
// Check for non-existing item
bool existsKiwi = fruits.Exists(f => f.Equals("kiwi"));
Console.WriteLine("Kiwi exists: " + existsKiwi);
}
}
The output of the above code is
Apple exists in the list Kiwi exists: False
Using Exists with Complex Conditions
Example
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Check if any number is greater than 5
bool existsGreaterThan5 = numbers.Exists(n => n > 5);
Console.WriteLine("Number greater than 5 exists: " + existsGreaterThan5);
// Check if any even number exists
bool existsEven = numbers.Exists(n => n % 2 == 0);
Console.WriteLine("Even number exists: " + existsEven);
// Check if any number is negative
bool existsNegative = numbers.Exists(n => n < 0);
Console.WriteLine("Negative number exists: " + existsNegative);
}
}
The output of the above code is
Number greater than 5 exists: True Even number exists: True Negative number exists: False
Using Exists with Custom Objects
Example
using System;
using System.Collections.Generic;
public class Student {
public string Name { get; set; }
public int Age { get; set; }
public double Grade { get; set; }
public Student(string name, int age, double grade) {
Name = name;
Age = age;
Grade = grade;
}
}
class Program {
static void Main(string[] args) {
List<Student> students = new List<Student>() {
new Student("Alice", 20, 85.5),
new Student("Bob", 22, 92.0),
new Student("Charlie", 19, 78.5)
};
// Check if any student has grade above 90
bool existsHighGrade = students.Exists(s => s.Grade > 90);
Console.WriteLine("Student with grade above 90 exists: " + existsHighGrade);
// Check if any student is under 20
bool existsYoung = students.Exists(s => s.Age < 20);
Console.WriteLine("Student under 20 exists: " + existsYoung);
// Check if specific student exists
bool existsAlice = students.Exists(s => s.Name == "Alice");
Console.WriteLine("Alice exists: " + existsAlice);
}
}
The output of the above code is
Student with grade above 90 exists: True Student under 20 exists: True Alice exists: True
Comparison with Other Methods
| Method | Purpose | Return Type |
|---|---|---|
Exists() |
Checks if any element matches condition | bool |
Find() |
Returns first element that matches condition | T |
FindAll() |
Returns all elements that match condition | List<T> |
Contains() |
Checks if specific value exists | bool |
Conclusion
The Exists method provides an efficient way to check if any element in a List<T> satisfies a given condition. It returns true as soon as the first matching element is found, making it more efficient than iterating through the entire collection manually.
