List <T>有什么好处.找到替代品?

Lly*_*yle 0 .net c# performance

最近我使用谓词来描述搜索逻辑并将其传递给一些列表的Find方法.

foreach (IHiscoreBarItemView item in _view.HiscoreItems)
{
    Predicate<Hiscore> matchOfHiscoreName = 
        (h) => h.Information.Name.Equals(item.HiscoreName);

    var current = player.Hiscores.Find(matchOfHiscoreName);
    item.GetLogicEngine().ForceSetHiscoreValue(current as Skill);

    var goal = player.Goals.Find(matchOfHiscoreName);
    item.GetLogicEngine().ForceSetGoalHiscoreValue(goal as Skill);
}
Run Code Online (Sandbox Code Playgroud)

除了"更少的代码"之外,使用上述方法替代替代方案是否有任何好处.

我对表现特别感兴趣.

谢谢

Jon*_*eet 12

使用LINQ查找的好处:它在.NET 2.0中可用LINQ over Find的好处:与其他序列的一致性; 查询表达式语法等

Find over BinarySearch的好处:列表不需要排序,你只需要相等比较BinarySearch优于Find:BinarySearch是O(log n); 找到是O(n)

查找foreach循环的好处:紧凑而不重复自己foreach循环的好处查找:您要执行的任何其他自定义处理

其中,只有Find vs BinarySearch有任何真正的性能差异.当然,如果你可以从a更改List<T>为a Dictionary<TKey,TValue>然后查找元素将被摊销O(1)...