过滤器列表<>对象没有在C#2.0中使用foreach循环 [英] Filter List<> object without using foreach loop in C#2.0
本文介绍了过滤器列表<>对象没有在C#2.0中使用foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何可以过滤列表与LT对象;在C#>
How we can filter the object in List<> in C#?
推荐答案
让我们说我们有一个列表<字符串方式>
键,你只想要的物品,其中字符串的长度大于5
Let's say we have a List<string>
and you want only the items where the length of the string is greater than 5.
下面的代码将返回一个列表<串>
的结果:
The code below will return a List<string>
with the results:
List<string> myList = new List<string>();
myList.Add("hello");
myList.Add("world!");
myList.Add("one");
myList.Add("large!!");
List<string> resultList = myList.FindAll(delegate(string s) { return s.Length > 5; });
resultList将containt的世界!和大!。
此示例使用匿名方法。它也可以写为:
List<string> myList = new List<string>();
// ..
List<string> resultList = myList.FindAll(OnlyLargerThanFive);
//..
private static bool OnlyLargerThanFive(string s)
{
return s.Length > 5;
}
委托以上,OnlyLargerThanFive,也被称为谓词
这篇关于过滤器列表<>对象没有在C#2.0中使用foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文