views:

175

answers:

1

Recently I used a predicate to describe search logic and passed it to the Find method of a few Lists.

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);
}

Are there any benefits, apart from 'less code', from using the aforementioned approach over an alternative.

I am particularly interested in performance.

Thanks

+8  A: 

Benefit of Find over using LINQ: It's available in .NET 2.0 Benefit of LINQ over Find: consistency with other sequences; query expression syntax etc

Benefit of Find over BinarySearch: The list doesn't have to be sorted, and you only need equality comparison Benefit of BinarySearch over Find: BinarySearch is O(log n); Find is O(n)

Benefit of Find over a foreach loop: compactness and not repeating yourself Benefit of a foreach loop over Find: Any other custom processing you want to perform

Of these, only Find vs BinarySearch has any real performance difference. Of course, if you could change from a List<T> to a Dictionary<TKey,TValue> then finding elements would become amortized O(1) ...

Jon Skeet
That's a very thorough answer, thanks.
vanslly