Skip to content

Latest commit

 

History

History
85 lines (51 loc) · 4.56 KB

convert-for-loop-to-foreach.md

File metadata and controls

85 lines (51 loc) · 4.56 KB
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
CSharp
VB

Refactoring to convert between a for loop and a foreach statement

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.

Convert a for loop to a foreach statement

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.

Why convert

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.

How to use it

  1. Place your caret in the for keyword.

  2. Press Ctrl+. or click the screwdriver Screwdriver icon icon in the margin of the code file.

    Convert to foreach menu

  3. Select Convert to 'foreach'. Or, select Preview changes to open the Preview Changes dialog, and then select Apply.

Convert a foreach statement to a for loop

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

Why convert

Reasons you might want to convert a foreach statement to a for loop include:

How to use it

  1. Place your caret in the foreach or For Each keyword.

  2. Press Ctrl+. or click the screwdriver Screwdriver icon icon in the margin of the code file.

    Convert to for menu

  3. Select Convert to 'for'. Or, select Preview changes to open the Preview Changes dialog, and then select Apply.

  4. 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.

See also