Partager via


Rename a code symbol refactoring

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This refactoring applies to:

  • C#

  • Visual Basic

What: Lets you rename identifiers for code symbols, such as fields, local variables, methods, namespaces, properties and types.

When: You want to safely rename something without having to find all instances, and copy/paste the new name.

Why: Copy and pasting the new name across an entire project would likely result in errors. This refactoring tool will accurately perform the renaming action.

How-to

  1. Highlight or place the text cursor inside the item to be renamed:

    • C#:

      Highlighted code - C#

    • Visual Basic:

      Highlighted code - Visual Basic

  2. Next, use your keyboard or mouse as follows:

    • Keyboard
      • Press Ctrl+R, then Ctrl+R. (Note that your keyboard shortcut may be different based on which profile you've selected.)
    • Mouse
      • Select Edit > Refactor > Rename.
      • Right-click the code and select Rename.
  3. Rename the item simply by typing the new name.

    • C#:

      Rename animation - C#

    • Visual Basic:

      Rename - VB

    Tip

    You can also update comments and other strings to use this new name, as well as preview the changes before saving, using the checkboxes in the Rename box that appears at the top right of your editor.

  4. When you're happy with the change, choose the Apply button or press Enter and the changes will be committed.

Remarks

  • Starting in Visual Studio 2019 version 16.3, when you rename a type that matches the name of the file it's in, a checkbox appears that enables you to rename the file at the same time. This option appears when you rename a class, interface, or enumeration. This option is not supported for partial types with multiple definitions.

    Rename animation with file - C#

  • If you use a name that already exists which would cause a conflict, the Rename box will warn you.

    Rename Conflict

  • Another way to rename a symbol is to change its name in the editor. Then, with the cursor in the symbol name, press Ctrl+. or just expand the light bulb icon menu that appears and choose Rename <old name> to <new name>.

    Rename in the editor

See also