- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Find a specific element in a C# List
Set a list −
List<int> myList = new List<int>() { 5, 10, 17, 19, 23, 33 };
Let us say you need to find an element that is divisible by 2. For that, use the Find() method −
int val = myList.Find(item => item % 2 == 0);
Here is the complete code −
Example
using System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List<int> myList = new List<int>() { 5, 10, 17, 19, 23, 33 }; Console.WriteLine("List: "); foreach(int i in myList) { Console.WriteLine(i); } int val = myList.Find(item => item % 2 == 0); Console.WriteLine("Element that divides by zero: "+val); } }
Output
List: 5 10 17 19 23 33 Element that divides by zero: 10
Advertisements