Aracılığıyla paylaş


Rename

Rename is a refactoring feature in the Visual Studio integrated development environment (IDE) 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 and to change the declarations and calls of an identifier.

Note

When using Source Control for Visual Studio, get the latest version of sources before you try to perform rename refactoring.

Rename refactoring is available from the following Visual Studio features:

Feature

Behavior of Refactoring in the IDE

Code Editor

In the Code Editor, rename refactoring is available when you position the cursor on certain types of code symbols. When the cursor is in this position, you can invoke the Rename command by typing the keyboard shortcut (CTRL + R, R), or by selecting the Rename command from a smart tag, shortcut menu, or the Refactor menu. When you invoke the Rename command, 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 shortcut 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 shortcut menu. If the selected source file contains a class whose class name is the same as the file name, 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, 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 in 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, the Rename command in Solution Explorer will only rename the source file and will not execute rename refactoring.

Rename Operations

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.

Note

When you rename an extension method, the rename operation propagates to all instances of the method that are in scope, regardless of whether the extension method is being used as a static method or an instance method. For more information, see Extension Methods (C# Programming Guide).

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 modify the property in the Project Designer. For more information, see Application Page.

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.

Remarks

Renaming Implemented or Overridden Members

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 cause 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.

interface IBase
{
    void Method();
}
public class Base
{
    public void Method()
    { }
    public virtual void Method(int i)
    { }
}
public class Derived : Base, IBase
{
    public new void Method()
    { }
    public override void Method(int i)
    { }
}
public class C : IBase
{
    public void Method()
    { }
}

In the previous example, 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 cause build errors.

Renaming Properties of Anonymous Types

When you rename a property in anonymous types, the rename operation will propagate to properties in other anonymous types that have the same properties. The following examples illustrate this behavior.

var a = new { ID = 1};
var b = new { ID = 2};

In the preceding code, renaming ID will change ID in both statements because they have the same underlying anonymous type.

var companyIDs =
    from c in companylist
    select new { ID = c.ID, Name = c.Name};

var orderIDs =
    from o in orderlist
    select new { ID = o.ID, Item = o.Name};

In the preceding code, renaming ID will only rename one instance of ID because companyIDs and orderIDs do not have the same properties.

See Also

Tasks

How to: Rename Identifiers

Concepts

Refactoring

Reference

Anonymous Types (C# Programming Guide)

Other Resources

Source Control for Visual Studio