- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a List contains the elements that match the specified conditions in C#?
To check whether a List contains the elements that match the specified conditions in C#, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { private static bool demo(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(demo)); } }
Output
This will produce the following output −
List elements... 255 315 410 500 600 710 800 1000 Does some elements match the predicate = True
Let us now see another example −
Example
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i) { return ((i % 7) == 0); } public static void Main(String[] args) { List<int> list = new List<int>(); list.Add(255); list.Add(310); list.Add(410); list.Add(500); list.Add(600); Console.WriteLine("List elements..."); foreach (int i in list) { Console.WriteLine(i); } Console.WriteLine("Does some elements match the predicate = "+list.Exists(demo)); } }
Output
This will produce the following output −
List elements... 255 310 410 500 600 Does some elements match the predicate = False
- Related Articles
- Check if an array contains the elements that match the specified conditions in C#
- How to get all elements of a List that match the conditions specified by the predicate in C#?
- How to check whether a List contains a specified element in C#
- Remove all elements of a List that match the conditions defined by the predicate in C#
- First occurrence in the List that matches the specified conditions in C#
- How to check whether an input string contains a specified substring ignoring the case using JSP?
- Check if a HashSet contains the specified element in C#
- Check whether the specified character has a surrogate code in C#
- Check whether the specified Unicode character is a punctuation mark in C#
- Check if SortedDictionary contains the specified key or not in C#
- How to insert the elements of a collection into the List at the specified index in C#?
- How to check in C# whether the string array contains a particular work in a string array?
- Check whether the Dictionary has the specified key or not in C#
- Check whether the Dictionary contains a specific value or not in C#
- How to check whether provided elements in an array have passed a specified condition or not in JavaScript?

Advertisements