Skip to main content

Introduce Alias

  • 2 minutes to read

Introduces a new file-scoped alias for the current type or namespace name, and replaces it with the newly-created alias.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on a reference to a type. This reference should contain namespace information.

#Notes

  • When applying this refactoring, you can choose whether to create an alias for the namespace where the referenced type is, or for the entire type reference.
  • Once an alias has been created, Rename is automatically invoked, so you can provide a proper alias name.
  • If you want to replace all matching references with the newly declared alias, use the Introduce Alias (replace all) refactoring.
  • This refactoring is the opposite of Replace with Alias.

#Example

using System;

namespace MyApplication
{
    class TestClass
    {
        private System.Drawing.Rectangle MyRec = new System.Drawing.Rectangle();
    }
}
Namespace MyApplication
    Public Class TestClass
        Private MyRec As System.Drawing.Rectangle = New System.Drawing.Rectangle()
    End Class
End Namespace

Result:

using System;
using RectangleAlias = System.Drawing.Rectangle;

namespace MyApplication
{
    class TestClass
    {
        private RectangleAlias MyRec = new RectangleAlias();
    }
}
Imports RectangleAlias = System.Drawing.Rectangle
Namespace MyApplication
    Public Class TestClass
        Private MyRec As RectangleAlias = New RectangleAlias()
    End Class
End Namespace

#Screenshot

rsIntroduceAlias

See Also