TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# List Find Method

This C# example demonstrates the Find method on the List type. It uses a lambda expression.

List find. A List can be searched imperatively.

This often involves a foreach-loop. It can be searched instead with the Find method: this often uses a lambda expression. Find makes code clearer in some program contexts.

Example. Instead of using a foreach-loop with an if-statement, you can use the Find instance method on List. Here we see that it also accepts a Predicate, which you can specify as a lambda expression. It returns the first match.

Based on:

.NET 4.5

C# program that uses Find method on List

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<int> list = new List<int>(new int[] { 19, 23, 29 });

	// Finds first element greater than 20
	int result = list.Find(item => item > 20);

	Console.WriteLine(result);
    }
}

Output

23

This code loops through each int value in the List, starting at the beginning, and tests each one to see if it is greater than 20. The value 23 is returned. The parameter to the Find method is a lambda expression: a Predicate instance.

Note: Please see the article on the Predicate type for more specific examples of its usage.

Predicate

To search backwards, use the FindLast method, which would return 29 in the example above. It will scan the List from the last element to the first. There are also FindIndex and FindLastIndex methods you can use.

Discussion. Another useful method on the List type that can be used to search a List is the Exists method. This receives a Predicate parameter and returns a bool value indicating whether the element was found.

List Exists

FindAll: The FindAll method on List, which is an instance method that returns a new List with the same element type, is also available.

Tip: If you want to find all the matching elements based on a Predicate, FindAll is useful.

Loops. In situations where the exact behavior of the List code is important, you can use for and foreach loops with List. This can sometimes also be faster than methods that receive delegates.

ListDelegates

Summary. A List can be searched with built-in methods and lambda expressions. These methods are convenient and fast. They can help you place the emphasis on other logic in your program. They reduce syntax noise.

List Contains


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf