C # list find

Source: Internet
Author: User

AListCan be searched imperatively. This often involves a foreach-loop. It can be searched instead withFindMethod: This often uses a Lambda expression. Find makes code clearer in some program contexts. It sometimes makes maintenance easier.

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.

Program that uses Find method on List [C#]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 20int result = list.Find(item => item > 20);Console.WriteLine(result);    }}Output23

This CodeLoops through each int value in the list, starting at the beginning, and tests each one to see if it is greater than 20. the first one that is, 23, is returned. the parameter to the find method is a Lambda expression that is considered a predicate instance.

Predicate type

Tip:Please see the article on the predicate type for more specific examples.

To search backwards,Use the findlast method, which wowould return 29 in the example above. it will scan the list from the very last element to the first. there are also findindex and findlastindex methods you can use, which wowould return the index of the matches, which wowould correspond to the numbers returned in the example.

Exists

Another useful method on the list type that can be used to search a list is the exists method. This variable es a predicate parameter and returns a bool value indicating whether the element was found.

List exists Method

Findall

The findall Method on list, which is an instance method that returns a new list with the same element type, is also available. if you want to find all the matching elements based on a predicate, this is useful.

Loop with List

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.

List examples

Delegate tutorial

Summary

In this article, we saw ways to find elements by searching in the list collection from system. collections. generic. these methods are convenient and can help you place the emphasis on other logic in your class.

Note:I use them extensively in programs with lists, using t in performance-critical situations.

List contains Method

List indexof Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.