Rename

Rename is a refactoring operation that provides an easy way to rename identifiers for code symbols such as fields, local variables, methods, namespaces, properties, and types. Rename can be used to change the names in comments and in strings in addition to the declarations and calls of an identifier.

Rename refactoring is available from the following Visual Studio features:

Feature

Behavior of Refactoring in the Development Environment

Code Editor

In the Code Editor, rename refactoring is available when you position the cursor on the code symbol declaration. When the cursor is in this position, you can invoke the Rename command by typing the keyboard shortcut, or by selecting the Rename menu item from a smart tag, context menu, or the Refactor menu. When you select the Rename menu item, the Rename dialog box appears. For more information, see Rename Dialog Box and How to: Rename Identifiers.

Class View

When you select an identifier in Class View, rename refactoring is available from the context menu and Refactor menu.

Object Browser

When you select an identifier in Object Browser, rename refactoring is only available from the Refactor menu.

Property Grid of the Windows Forms Designer

In the Property Grid of the Windows Forms Designer, changing the name of a control will initiate a rename operation for that control. The Rename dialog box will not appear.

Solution Explorer

In Solution Explorer, a Rename command is available on the context menu. If the selected source file contains a class whose class name is the same as the file name, then you can use this command to simultaneously rename the source file and execute rename refactoring.

For example, if you create a default Windows application and then rename Form1.cs to TestForm.cs, then the source file name Form1.cs will change to TestForm.cs and the class Form1 and all references to that class will be renamed to TestForm.

Note

The Undo command (CTRL+Z) will only undo rename refactoring within the code and will not change the file name back to the original name.

If the selected source file does not contain a class whose name is the same as the file name, then the Rename command in Solution Explorer will only rename the source file and will not execute rename refactoring.

When you execute Rename, the refactoring engine performs a rename operation specific for each code symbol, described in the following table.

Code Symbol

Rename Operation

Field

Changes the declaration and usages of the field to the new name.

Local variable

Changes the declaration and usages of the variable to the new name.

Method

Changes the name of the method and all references to that method to the new name.

Namespace

Changes the name of the namespace to the new name in the declaration, all using statements, and fully qualified names.

Note

When renaming a namespace, Visual Studio also updates the Default Namespace property on the Application Page of the Project Designer. This property cannot be reset by selecting Undo from the Edit menu. To reset the Default Namespace property value, you must edit the property in the Project Designer.

Property

Changes the declaration and usages of the property to the new name.

Type

Changes all declarations and all usages of the type to the new name, including constructors and destructors. For partial types, the rename operation will propagate to all parts.

When you Rename a member that either implements/overrides or is implemented/overridden by members in other types, Visual Studio displays a dialog box that says the rename operation will result in cascading updates. If you click continue, the refactoring engine recursively finds and renames all members in base and derived types that have implements/overrides relationship with the member being renamed.

The following code example contains members with implements/overrides relationship.

In the example above, renaming C.Method() also renames Ibase.Method() because C.Method() implements Ibase.Method(). Next, the refactor engine recursively sees that Ibase.Method() is implemented by Derived.Method() and renames Derived.Method(). The refactor engine does not rename Base.Method(), because Derived.Method() does not override Base.Method(). The refactoring engine stops here unless you have Rename overloads checked in the Rename dialog box.

If Rename overloads is checked, the refactor engine renames Derived.Method(int i) because it overloads Derived.Method(), Base.Method(int i) because it is overridden by Derived.Method(int i), and Base.Method() because it is an overload of Base.Method(int i).

Note

When you rename a member that was defined in a referenced assembly, a dialog box explains that renaming will result in build errors.