The requested page is not available for the requested platform. You are viewing the content for Default platform.

Safe Rename

  • 2 min to read

Renames a non-private method or property in the class in a manner that will not break any code dependent upon the old name (such as code in different assemblies).

Purpose

When classes in one assembly depend upon classes in another assembly, it's easy to break things by renaming a public or protected member. This inter-assembly dependency is common in frameworks, plug-in systems, and other highly-decoupled software architectures. Note that in .NET it is possible to have a dependency between assemblies which are members of different solutions.

Availability

Available from the context menu or via shortcuts:

  • when the edit cursor, or caret is on a public method or property declaration.

Notes

  • Safe Rename declares a new method right above the source method.
  • Safe Rename declares a new method copying the signature and method body from the old method. The source method's body simply calls the newly declared method after applying this refactoring.
  • Safe Rename applies the Obsolete attribute to the source method which means that the compiler will now show warnings when compiling any code that refers to the old method.

Options

  • You can change the template for the warning message. This template is used to build the Obsolete attribute's content automatically.
  • You can specify the prefix that will be automatically added to the newly declared methods' names.

Example

public class Class1
{
    public int TestMethod(int a, int b)
    {
        return a + b;
    }
}
Public Class Class1
    Public Function TestMethod(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a + b
    End Function
End Class

Result:

public class Class1
{
    [Obsolete("Use NewMethod instead.")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public int TestMethod(int a, int b)
    {
        return NewMethod(a, b);
    }
    public int NewMethod(int a, int b)
    {
        return a + b;
    }
}
If a Then
Public Class Class1
    <Obsolete("Use NewMethod instead."), _
    EditorBrowsable(EditorBrowsableState.Never)> _
    Public Function TestMethod(ByVal a As Integer, ByVal b As Integer) As Integer
        Return NewMethod(a, b)
    End Function
    Public Function NewMethod(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a + b
    End Function
End Class

Animation

CSharpSafeRename

See Also