Visual Studio – Edit Menu Refactor options

Visual Studio provides very useful tools for doing a certain amount of code refactoring. Refactoring is the task of changing your source code without affecting its functionality.

If you look at the Edit Menu in VS, you will find 6 refactoring options:

















Lets look at each one of them with examples:


Rename lets you change the name of a variable or an object or a class anywhere in your code. You select the text in your editor and choose the Rename menu option. It will ask you enter the new name for the object:













Then it will show a preview of the changes that will be effected:













Clicking on Apply will make the changes permanent.


Extract Method

Extract Method is used for taking some code out of an existing function or method and converting it into a separate method. This is probably the most common and useful cases of refactoring, where you find that a certain piece of code has been repeated or needs to be callable from a lot of places.

In the sample below, we select some code to refactor:















and we choose a new method into which the code will go into. You can enter the name of the method to what you want . Notice that the original selected code gets replaced by the new method call.















Encapsulate Field

If you have a class level attribute and want to convert into a field with get/set methods, then this method is useful as it saves you a lot of typing.

Select the variable . It will automatically prefix a capital M to the name. You can change that if you wish.













After execution, the original variable declaration remains and a new get/set method pair is added.









Extract Interface

If you want to create an interface for any public method in a class, then this option makes it easy . Choosing this option from the Edit menu will show you the public methods in the current class in your editor:














You can then choose the name of the Interface and which method(s) you want to declare an interface for. A new file will be created with the interface declaration:










Remove Parameters

As the name suggests this is used to remove some parameters from a function or method call. Be warned that it will  do a syntactical change only all across the source code. Your code may stop working or have unexpected results if the logic changes due to this operation. Select the function you want to remove parameter(s) for:











It will show you a preview of the changes that will be done. Click Apply to make changes permanent.












Reorder Parameters

Reordering parameters is used for changing the sequence of the arguments being called in a function. Select the function you want to change it for. Then use the arrow keys to change the order of the parameters.













Clicking on Apply in the preview screen makes the changes permanent.













This completes the explanation of the Refactor options in VS. Note that even if you save the changes after a Refactor you can use the Undo function to revert the original code back, except for  Extract Interface , where the new file created will not be undone. You will have to manually delete it.







1 Comment

Leave a Reply

Your email address will not be published.