Escolar Documentos
Profissional Documentos
Cultura Documentos
http://msdn.microsoft.com/en-us/library/6kxxabwd
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
Code Editor
Class View
Object Browser
1 of 6
5/25/2012 11:37 AM
http://msdn.microsoft.com/en-us/library/6kxxabwd
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, as described in the following table.
Code Symbol
Field Local variable Method
Rename Operation
Changes the declaration and usages of the field to the new name. Changes the declaration and usages of the variable to the new name.
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 Type
Changes the declaration and usages of the property to the new name. 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.
To rename an identifier
1. Create a console application named RenameIdentifier, and then replace Program with the following example code.
2 of 6
5/25/2012 11:37 AM
http://msdn.microsoft.com/en-us/library/6kxxabwd
class ProtoClassA { // Invoke on 'MethodB'. public void MethodB(int i, bool b) { } } class ProtoClassC { void D() { ProtoClassA MyClassA = new ProtoClassA(); // Invoke on 'MethodB'. MyClassA.MethodB(0, false);
2. Place the cursor on MethodB, either in the method declaration or the method call. 3. From the Refactor menu, select Rename. The Rename dialog box appears. You can also type the keyboard shortcut F2 to display the Rename dialog box. You can also right-click the cursor, point to Refactor on the context menu, and then click Rename to display the Rename dialog box. 4. In the New Name field, type MethodC. 5. Select the Search in Comments check box. 6. Click OK. 7. In the Preview Changes dialog box, click Apply.
class ProtoClassA { // Invoke on 'MethodB'. public void MethodB(int i, bool b) { } } class ProtoClassC { void D() { ProtoClassA MyClassA = new ProtoClassA(); // Invoke on 'MethodB'. MyClassA.MethodB(0, false);
3 of 6
5/25/2012 11:37 AM
http://msdn.microsoft.com/en-us/library/6kxxabwd
2. In the declaration for MethodB, type or backspace over the method identifier. A smart tag prompt will appear below this identifier.
Note
You can only invoke rename refactoring using smart tags at the declaration of an identifier. 3. Type the keyboard shortcut SHIFT+ALT+F10, and then press the DOWN ARROW to display the smart tag menu. -orMove the mouse pointer over the smart tag prompt to display the smart tag. Then move the mouse pointer over the smart tag and click the DOWN ARROW to display the smart tag menu. 4. Select the Rename '<identifer1>' to '<identifier2>' menu item to invoke rename refactoring without a preview of the changes to your code. All references to <identifer1> will automatically be updated to <identifier2>. -orSelect the Rename with preview menu item to invoke rename refactoring with a preview of the changes to your code. The Preview Changes dialog box will appear.
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 relationships with the member being renamed. The following code example contains members with implements/overrides relationships.
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
4 of 6
5/25/2012 11:37 AM
http://msdn.microsoft.com/en-us/library/6kxxabwd
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.
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
Reference Anonymous Types (C# Programming Guide)
5 of 6
5/25/2012 11:37 AM
http://msdn.microsoft.com/en-us/library/6kxxabwd
Yes
No
Community Content
2012 Microsoft. All rights reserved.
6 of 6
5/25/2012 11:37 AM