Declaring a variable inside or outside an foreach loop: which is faster/better?

Performance-wise both examples are compiled to the same IL, so there’s no difference.

The second is better, because it more clearly expresses your intent if u is only used inside the loop.

Leave a Comment