Accessing the Rename Operation

You can rename from many places within the IDE. In the section “Invoking the Refactoring Tools,” earlier in this chapter, we looked at accessing Refactoring operations from the Quick Actions (light bulb) menu. You get this menu when you rename a type or method in the code editor. This allows you to apply the rename to the other code elements that reference the renamed item. You can also access Rename by right-clicking an element in the code editor. In addition, if you use the Properties dialog box to change the name of a control you’ve placed on a form or an element within class view. In both instances the Rename operation is invoked behind the scenes, and the item is renamed appropriately.

As an example, from the Class View (View, Class View) you can access the Rename operation via the Properties window of a given code element. Figure 9.6 shows an example of the Class View, Properties window, and Rename operation working together. Renaming an item here invokes the full rename for all code that references the given element being renamed (without preview).

Image

FIGURE 9.6 You can access Rename from the Class View and Properties window.

You can also rename directly within Solution Explorer for a filename that equates to a class name. For instance, suppose you have a file named Customer and you want to change the class name and filename to Shopper. You do so by right-clicking and choosing Rename. Visual Studio enables you to rename the file. When you do so, it prompts you to see whether you also want to rename the class. If you choose yes, Visual Studio refactors your code on your behalf. So if you have a Customer class and a Customer.cs file, a Rename operation to Shopper will rename the file as well as the class if you give it permission to (and will refactor all references to the previous class name).


Note

Although an undo on the Rename operation rolls back a change, in the case of a filename change, Undo reverts the code but does not change the filename back to its original name.


Rename Keyboard Shortcuts

You can access the Rename operation from keyboard shortcuts. Rename is a popular task, so Visual Studio has provided its own shortcut key: F2. Previous versions of Visual Studio required you to invoke rename via the shortcut “chord,” Ctrl+R, R (where you continue to hold Ctrl when pressing the second R). Pressing this combination in sequence still brings up the Refactoring, Rename operation relative to the code element behind your cursor. Figure 9.7 shows an example of using the shortcut key to rename the Customer class. Notice the editor highlights the item being renamed. You now simply type the new name in the editor and hit the Apply button as shown.

Image

FIGURE 9.7 Use F2 or Ctrl+R, R to invoke rename from the keyboard.

Rename from Quick Actions

You can also invoke rename directly from the Quick Actions menu. As an example, suppose we are trying to rename an enum from OrderStatus to OrderState. Typing over the old name in the code editor invokes the light bulb menu, as shown in Figure 9.8. Notice that directly from this light bulb menu you can choose to apply the rename or execute the rename with preview. Notice also the squiggled line shown over OrderStatus above, showing a compile error relative to the element being renames. Figure 9.9 shows the Preview dialog after selecting the operation from the light build menu.

Image

FIGURE 9.8 Renaming an element in the code editor invokes the light bulb menu.

Image

FIGURE 9.9 Previewing rename changes including comments and strings.


Note

Using the light bulb menu to rename does not give you the option to choose to rename comments or strings. You would need to invoke from the keyboard or shortcut menu to get that option.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset