C# List Find

本文介绍了C#中List集合查找元素的方法。可使用Find、FindLast等方法结合lambda表达式查找元素,还可使用Exists方法判断元素是否存在,FindAll方法查找所有匹配元素。在对性能要求不高时,这些方法方便实用,也可使用for和foreach循环。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 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 20
	int result = list.Find(item => item > 20);

	Console.WriteLine(result);
    }
}

Output

23

List type

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 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.

Reverse graphic

To search backwards, use the FindLast method, which would 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 would return the index of the matches, which would 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 receives a Predicate parameter and returns a bool value indicating whether the element was found.

List Exists Method

FindAll

Programming tip

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

The C# programming language

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, except in performance-critical situations.

List Contains Method

List IndexOf Method

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/08/15/2640396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值