List<T> Exist & Find question

I have a question about the Exist & Find methods in a List. Below is my if() statement as I’m using it now:

if(myChar.SkillList.Exists(x=> x.Skill==d20Skill.Tumble) && myChar.SkillList.Find(x=> x.Skill==d20Skill.Tumble).Ranks>=5) tempDC+=2;

But will the next code give the same result (not yet at the point of testing the actual code)

if(myChar.SkillList.Exists(x=> x.Skill==d20Skill.Tumble && Ranks>=5)) tempDC+=2;

…yes, I am coding the d20 RPG system for Unity for my upcoming RPG :wink:

They would give the result, but they aren’t the same. The first pointlessly iterates over at least twice as many elements as the second, when a simple For loop and manual checking would be faster. The second is a shortcut for exactly the same logic that would go into the For loop, so it’s fine.

Lambda extensions methods for generic collections are really cool and fantastically easy to use, but if you’re using 2 or more in combination then doing it by hand is often faster, or at the very least easier to read (which is even more important, IMO).

1 Like

Thanks for confirming they give the same result. Though not using a loop to evaluate the data (and thus speed not being an issue), I’ll stick with the (somewhat less easier to read) 2nd method. That whole d20 system is already getting way too big and where I can save on (typed) space I certainly will. Not to mention, I’m documenting it all already and the d20 system itself is already pretty well documented…