Home  >  Article  >  Backend Development  >  C# program shows the use of Exists property

C# program shows the use of Exists property

WBOY
WBOYforward
2023-08-22 23:53:021002browse

C# program shows the use of Exists property

The Exists property is a very useful property in C#, which checks whether any element in the collection meets a given condition. This property is part of the List class in C# and returns a Boolean value indicating whether there are any elements in the list that meet the specified criteria. In this article, we will explore the usage of Exists property in C# programs.

What is the Exists attribute?

The Exists property is a Boolean property defined in the C# List class. It accepts a delegate as argument and returns a Boolean value indicating whether there are any elements in the list that match the given criteria.

Syntax for existing attributes

public bool Exists(Predicate<T> match)

Example: Use the Exists property to check if any element exists in the list

Let's see an example that demonstrates how to use the Exists property to check if any element exists in a 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 called fruits. We use the Exists property to check if the element "apple" exists in the list. We pass a lambda expression that checks if each element in the list is equal to "apple".

Output

Apple exists in the list

Example: Use the Exists attribute to check if there are any elements that meet the condition

Now, let's look at an example that demonstrates 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 called numbers. We use the Exists property to check if any element in the list is greater than 3. We passed a lambda expression that checks if each element in the list is greater than 3.

Output

There exists an element in the list greater than 3

in conclusion

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

The above is the detailed content of C# program shows the use of Exists property. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete