title | description | ms.date | ms.topic | author | ms.author | manager | ms.subservice | dev_langs | ||
---|---|---|---|---|---|---|---|---|---|---|
Refactor to convert a for loop to a foreach statement |
Learn how to use the Quick Actions and Refactorings menu to convert between a for loop and a foreach statement. |
03/10/2020 |
reference |
mikadumont |
midumont |
mijacobs |
general-ide |
|
This article describes the Quick Actions refactorings that convert between two looping structures. It includes some reasons why you might want to switch between a for loop and a foreach statement in your code.
If you have a for loop in your code, you can use this refactoring to convert it to a foreach statement.
This refactoring applies to:
-
C#
-
Visual Basic
Note
The Convert to foreach Quick Action refactoring is only available for for loops that contain all three parts: an initializer, condition, and iterator.
Reasons you might want to convert a for loop to a foreach statement include:
-
You don't use the local loop variable inside the loop except as an index to access items.
-
You want to simplify your code and reduce the likelihood of logic errors in the initializer, condition, and iterator sections.
-
Place your caret in the
for
keyword. -
Press Ctrl+. or click the screwdriver
icon in the margin of the code file.
-
Select Convert to 'foreach'. Or, select Preview changes to open the Preview Changes dialog, and then select Apply.
If you have a foreach (C#) or For Each...Next (Visual Basic) statement in your code, you can use this refactoring to convert it to a for loop.
This refactoring applies to:
-
C#
-
Visual Basic
Reasons you might want to convert a foreach statement to a for loop include:
-
You want to use the local loop variable inside the loop for more than just accessing the item.
-
You are iterating through a multi-dimensional array and you want more control over the array elements.
-
Place your caret in the
foreach
orFor Each
keyword. -
Press Ctrl+. or click the screwdriver
icon in the margin of the code file.
-
Select Convert to 'for'. Or, select Preview changes to open the Preview Changes dialog, and then select Apply.
-
Because the refactoring introduces a new iteration count variable, the Rename box appears at the top-right corner of the editor. If you want to choose a different name for the variable, type it in and then press Enter or select Apply in the Rename box. If you don't want to choose a new name, press Esc or select Apply to dismiss the Rename box.
Note
For C#, the code generated by these refactorings uses either an explicit type or var for the type of the items in the collection. The type in the generated code, explicit or implicit, depends on the code-style settings that are in scope. These particular code-style settings are configured at the machine level under Tools > Options > Text Editor > C# > Code Style > General > 'var' preferences, or at the solution level in an EditorConfig file. If you change a code-style setting in Options, reopen the code file for the changes to take effect.