Microsoft Visual Studio 2019 - Code refactoring

Before VS 2019, also we have quick actions to refactor the code. Microsoft made a lot of improvements to it in Visual Studio 2019, which provides more suggestions for your code refactoring. As we already know, light bulb displays when we place the cursor on code as below.

Select the bulb or press Ctrl+., it displays different suggestions for your code.

 

As shown above, here we got four different suggestions Fix formatting, Invert if, Wrapping, Configure or Suppress issues. You can select these options based on your requirement to refactor the specific code block.

When we select the “Invert if” option for our code, it refactors the code as below. You can see the preview of the changes and can apply to your code if you are happy with the changes.

Click on the Apply button to apply the changes to your codebase.

We can wrap and unwrap arguments for code visibility in Visual Studio 2019. If any method takes more than one argument, wrapping arguments increases code visibility.

                       

As shown above, all three arguments are displaying in separate lines, which increases the code visibility.

We can also unwrap the arguments list and place it in a single line using the quick actions menu as below; it also Unwraps and indents all arguments.

 

                            

VS 2019 also provides suggestions to pull members from child class to parent class.

 

                                

Here it provided suggestions to move Id property from Emp class (child class) to Company class (parent class).

Microsoft Visual Studio 2019 also provides suggestions to refactor the foreach loop to use  LINQ queries.

 

                         

Here foreach loop on listEmps refactored to LINQ query.