- 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
First occurrence in the List that matches the specified conditions in C#
To get the first occurrence in the list that matches the specified conditions, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i){ return ((i % 10) == 0); } public static void Main(String[] args){ List<int> list = new List<int>(); list.Add(200); list.Add(215); list.Add(310); list.Add(500); list.Add(600); Console.WriteLine("List elements..."); foreach (int i in list){ Console.WriteLine(i); } Console.WriteLine("First Occurrence = "+list.Find(demo)); } }
Output
This will produce the following output −
List elements... 200 215 310 500 600 First Occurrence = 200
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i){ return ((i % 2) == 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("First Occurrence = "+list.Find(demo)); } }
Output
This will produce the following output −
List elements... 255 315 410 500 600 710 800 1000 First Occurrence = 410
- Related Articles
- Check if every List element matches the predicate conditions in C#
- How to check whether a List contains the elements that match the specified conditions in C#?
- Removing first occurrence of specified value from LinkedList in C#
- How to get all elements of a List that match the conditions specified by the predicate in C#?
- Check if an array contains the elements that match the specified conditions in C#
- C# program to remove the first occurrence of a node in a Linked List
- Remove the first occurrence from the StringCollection in C#
- Python - First occurrence of one list in another
- How to get the first index of an occurrence of the specified value in a string in JavaScript?
- Search for an element matching the conditions and return the zero-based index of the last occurrence within the entire List in C#
- Index of first occurrence in StringCollection in C#?
- Removing the specified element from the List in C#
- Remove all elements of a List that match the conditions defined by the predicate in C#
- Remove the first occurrence of a specific object from the ArrayList in C#
- Find the first node in LinkedList containing the specified value in C#

Advertisements