C# 在列表中查找匹配项的最简洁方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cleanest Way to Find a Match In a List
提问by Nick
What is the best way to find something in a list? I know LINQ has some nice tricks, but let's also get suggestions for C# 2.0. Lets get the best refactorings for this common code pattern.
在列表中查找内容的最佳方法是什么?我知道 LINQ 有一些不错的技巧,但让我们也获得有关 C# 2.0 的建议。让我们为这个常见的代码模式获得最好的重构。
Currently I use code like this:
目前我使用这样的代码:
// mObjList is a List<MyObject>
MyObject match = null;
foreach (MyObject mo in mObjList)
{
if (Criteria(mo))
{
match = mo;
break;
}
}
or
或者
// mObjList is a List<MyObject>
bool foundIt = false;
foreach (MyObject mo in mObjList)
{
if (Criteria(mo))
{
foundIt = true;
break;
}
}
采纳答案by Konrad Rudolph
@ Konrad: So how do you use it? Let's say I want to match mo.ID to magicNumber.
@康拉德:那你怎么用呢?假设我想将 mo.ID 与 magicNumber 匹配。
In C# 2.0 you'd write:
在 C# 2.0 中,你会这样写:
result = mObjList.Find(delegate(int x) { return x.ID == magicNumber; });
3.0 knows lambdas:
3.0 知道 lambdas:
result = mObjList.Find(x => x.ID == magicNumber);
回答by Konrad Rudolph
Put the code in a method and you save a temporary and a break
(and you recycle code, as a bonus):
将代码放在一个方法中,然后保存一个临时和一个break
(并且你回收代码,作为奖励):
T Find<T>(IEnumerable<T> items, Predicate<T> p) {
foreach (T item in items)
if (p(item))
return item;
return null;
}
… but of course this method already exists anyway for Lists, even in .NET 2.0.
……当然,这种方法已经存在于列表中,即使在 .NET 2.0 中也是如此。
回答by Todd
Using a Lambda expression:
使用 Lambda 表达式:
List<MyObject> list = new List<MyObject>();
// populate the list with objects..
return list.Find(o => o.Id == myCriteria);
回答by Nick
Evidently the performance hit of anonymous delegates is pretty significant.
显然,匿名代表的性能影响非常显着。
Test code:
测试代码:
static void Main(string[] args)
{
for (int kk = 0; kk < 10; kk++)
{
List<int> tmp = new List<int>();
for (int i = 0; i < 100; i++)
tmp.Add(i);
int sum = 0;
long start = DateTime.Now.Ticks;
for (int i = 0; i < 1000000; i++)
sum += tmp.Find(delegate(int x) { return x == 3; });
Console.WriteLine("Anonymous delegates: " + (DateTime.Now.Ticks - start));
start = DateTime.Now.Ticks;
sum = 0;
for (int i = 0; i < 1000000; i++)
{
int match = 0;
for (int j = 0; j < tmp.Count; j++)
{
if (tmp[j] == 3)
{
match = tmp[j];
break;
}
}
sum += match;
}
Console.WriteLine("Classic C++ Style: " + (DateTime.Now.Ticks - start));
Console.WriteLine();
}
}
Results:
结果:
Anonymous delegates: 710000
Classic C++ Style: 340000
Anonymous delegates: 630000
Classic C++ Style: 320000
Anonymous delegates: 630000
Classic C++ Style: 330000
Anonymous delegates: 630000
Classic C++ Style: 320000
Anonymous delegates: 610000
Classic C++ Style: 340000
Anonymous delegates: 630000
Classic C++ Style: 330000
Anonymous delegates: 650000
Classic C++ Style: 330000
Anonymous delegates: 620000
Classic C++ Style: 330000
Anonymous delegates: 620000
Classic C++ Style: 340000
Anonymous delegates: 620000
Classic C++ Style: 400000
In every case, using anonymous delegates is about 100% slower than the other way.
在每种情况下,使用匿名委托比其他方式慢 100%。