博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# List Find
阅读量:6325 次
发布时间:2019-06-22

本文共 2490 字,大约阅读时间需要 8 分钟。

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
list = new List
(new int[] { 19, 23, 29 }); // Finds first element greater than 20 int result = list.Find(item => item > 20); Console.WriteLine(result); }}Output23

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.

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.

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.

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

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

你可能感兴趣的文章
clear .svn folder use bat
查看>>
c++ 类 总结
查看>>
java日期转字符串 字符串转日期 日期转日历 日历转日期
查看>>
SQL Server2008,解除sa禁用
查看>>
java 反射一
查看>>
hdu 2413(最大匹配+二分)
查看>>
CSS:用SASS(SCSS)实现栅格系统
查看>>
struts2的初步认识!
查看>>
.Net内存泄露原因及解决办法
查看>>
Nginx+Lua 积累
查看>>
Pass value from child popup window to parent page window using JavaScript--reference
查看>>
30个实用的jQuery选项卡/导航教程推荐
查看>>
AfxMessageBox和MessageBox差别
查看>>
php的isset()和empty()区别
查看>>
Dynamic CRM 2013学习笔记(二十九)报表设计:reporting service 报表开发常见问题
查看>>
django安装和卸载
查看>>
分享一个强大的采集类,还可以模拟php多进程
查看>>
【服务器推】主流方式
查看>>
(C++)C++类继承中的构造函数和析构函数
查看>>
Solr部署如何启动
查看>>