C# Program to Show the Use of Exists Property


The Exists property in C# is a very useful property that checks whether any element in a collection matches a given condition. This property is a part of the List<T> class in C# and returns a Boolean value indicating whether any element exists in the list that satisfies the specified condition. In this article, we will explore the usage of the Exists property in a C# program.

What is the Exists Property?

The Exists property is a Boolean property that is defined in the List<T> class in C#. It takes a delegate as an argument and returns a Boolean value indicating whether any element in the list matches the given condition.

Syntax of Exists Property

public bool Exists(Predicate<T> match)

Example: Using Exists Property to Check if any Element Exists in the List

Let's see an example of how to use the Exists property to check if any element exists in the list.

using System;
using System.Linq;
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");
      }
   }
}

In this code, we have a list of strings named fruits. We use the Exists property to check whether the element "apple" exists in the list. We pass a lambda expression that checks whether each element in the list is equal to "apple".

Output

Apple exists in the list

Example: Using Exists Property to Check if any Element Satisfies a Condition

Now, let's see an example of how to use the Exists property to check if any element in the list satisfies a condition.

using System;
using System.Linq;
using System.Collections.Generic;

class Program {
   static void Main(string[] args) {
      List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
   
      bool exists = numbers.Exists(n => n > 3);
   
      if (exists) {
         Console.WriteLine("There exists an element in the list greater than 3");
      }
      else {
         Console.WriteLine("There is no element in the list greater than 3");
      }
   }
}

In this code, we have a list of integers named numbers. We use the Exists property to check whether any element in the list is greater than 3. We pass a lambda expression that checks whether each element in the list is greater than 3.

Output

There exists an element in the list greater than 3

Conclusion

The Exists property is a powerful property that can be used to check whether any element in a collection satisfies a given condition. In this article, we explored the usage of the Exists property in a C# program. We saw how to check if an element exists in a list and how to check if any element in a list satisfies a condition.

Updated on: 04-May-2023

716 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements