There's a performance difference between for and foreach!? D:
*googles*
The for-loop is faster than the foreach-loop if the array must only be accessed once per iteration.
Alright, so nothing unexped!
I didn't mean that at all. That kind of overhead is fine.
What I mean is that entering a foreach involves allocating an iterator object, at least in mono. And if you have a heavily nested section that involves a foreach, such that the section can get hit thousands of time per frame, and you're going for 60fps, you're going to pile up an awful lot of temporary allocation. That's not a semantic problem, but it can lead to frequent garbage collection, which can be quite a bane. We ran into it in a serious way when porting AIW to unity, because the GC was happening so often it was chopping the music playback to pieces, nor was it great for general responsiveness.
When we converted some of the most frequently hit foreach's to for's, that problem mostly went away. There are other things like List<T>.Contains() where T is a value-type that cause similar problems, or anonymous delegates that involve local parameters from the calling scope, but step one was "stop using foreach"
So back to the Lamdbas: I guess you're right, in that I shouldn't try to optimise. Especially when all I stand to gain is concise code.
I think the optimization is fine, as long as you understand (and are willing to accept) what it's actually doing. And remember that the biggest optimization is from "not working" to "working".
Are you sure the problem was actually in the lambda, or was the list being iterated containing some nulls or whatever?