C# program shows the use of Exists property

WBOY
Release: 2023-08-22 23:53:02
forward
977 people have browsed it

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 match)
Copy after login

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 fruits = new List() { "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"); } } }
Copy after login

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
Copy after login

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 numbers = new List() { 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"); } } }
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!