Visual Studio Tip: Quickly convert for-to-foreach

Visual Studio 2017 IDE provides quick refactoring using Quick Actions and Refactoring. Refactoring is a process of reforming our code without changing its functional purpose and making it easier to maintain, read, and support.

There are a number of third-party extensions and tools that also allow for extensive and editor-level code refactoring. First of all, doing quick work with a lightbulb in the Visual Studio IDE is not new. Additionally, in different versions of Visual Studio, new code refactoring options are added to the IDE. Additionally, the IDE now supports refactoring for for-to-foreach conversions and vice versa.

Convert to ‘foreach’ using quick action:

You can find this new feature with Visual Studio 2017 version 15.7 Preview 4. While using for-to-foreach loop, you just need to find the quick action icon (shortcut Ctrl+.) It will show give you potential fixes.

Visual Studio Tip:  Quickly convert for-to-foreach

Publish quick actions, you can apply changes directly or make global changes in the Changes Preview window and publish by clicking Apply.

Visual Studio Tip:  Quickly convert for-to-foreach

Addition to for-to-foreach, you can also make private fields read-only using the quick refactoring in Visual Studio.

Quick actions let you easily refactor and write code with just one action. Quick Action Refactoring in Visual Studio is an extremely powerful productivity tool that you can leverage for a number of aspects. You can quickly add references to NuGet packages in Visual Studio by using Quick Actions in Visual Studio to upgrade your project’s language version.

Visual Studio Tip:  Quickly convert for-to-foreach

11 Effective Debugging Tips for .NET Developer



You can also apply code fixes to code analyzer rule violations by generating new code and applying different code refactoring.

You can check more article on our website under C# section HERE or you can also check same on Micrsoft Docs too.

Leave a Reply

Your email address will not be published. Required fields are marked *