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.
More Related Contents:
- Is there any alternate way of avoiding the Foreach loop? [closed]
- Performance loss caused by Linq [duplicate]
- not able to fetch text using utility in c#
- Is there a reason for C#’s reuse of the variable in a foreach?
- How do you get the index of the current iteration of a foreach loop?
- Is a LINQ statement faster than a ‘foreach’ loop?
- Update all objects in a collection using LINQ
- How do foreach loops work in C#? [closed]
- Captured Closure (Loop Variable) in C# 5.0
- How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?
- Has foreach’s use of variables been changed in C# 5?
- Performance difference for control structures ‘for’ and ‘foreach’ in C#
- Iterate Multi-Dimensional Array with Nested Foreach Statement
- How does foreach work when looping through function results?
- Modify Struct variable in a Dictionary
- What is the difference between for and foreach?
- Foreach loop, determine which is the last iteration of the loop
- Does foreach automatically call Dispose?
- How do I segment the elements iterated over in a foreach loop
- foreach with index [duplicate]
- Intelligent way of removing items from a List while enumerating in C#
- Why can’t we assign a foreach iteration variable, whereas we can completely modify it with an accessor?
- Lambda Expression using Foreach Clause [duplicate]
- Possible to iterate backwards through a foreach?
- Linq style “For Each” [duplicate]
- Check for null in foreach loop
- Starting a new thread in a foreach loop
- Why can’t I modify the loop variable in a foreach?
- Why can’t I do foreach (var Item in DataTable.Rows)?
- How do I copy items from list to list without foreach?