Refactoring
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
ReSharper's set of refactorings by far surpasses that provided by Visual Studio, with regard to number, usability and scope of application. Each of our code refactorings analyzes the entire scope of the code selection to which it is applied (which can be as wide as your whole solution), including cross-language code, and uses this insight to update the code structure in the most intelligent way possible. However, simple changes - usually applied locally - are actually code transformations and not real refactorings, as defined by M.Fowler and implemented in ReSharper. Code transformations, in their turn, are conveniently implemented in ReSharper as Quick-Fixes, Context Actions and other coding assistance features.
Click a button to display features available in specific edition.
Using Code Refactorings
To use a refactoring, you place the caret on a symbol or select a block of code and then invoke the refactoring you want from the ReSharper's Refactor menu, or by using its individual keyboard shortcut. Alternatively, the Refactor This command lets you see which refactorings can be applied at the current caret position or for the current selection; it also has a handy shortcut - Ctrl + Shift + R.
Rename Symbol
The Rename refactoring allows you to rename any symbol, including: namespaces, types, methods, parameters, local variables, properties, fields, and events. It automatically finds and corrects all references to the symbol. The Rename refactoring can be invoked directly from the editor and sometimes from other views (Class View, Object Browser).
Rename Symbol, in particular, works in all supported languages: C#, VB.NET, ASP.NET and XAML.
Move Type
You can easily move types between namespaces, with references automatically updated by ReSharper. You can also move a type to outer or nested scope or to another file.
Copy Type
It will take you just a second to create a copy of a type with a different name or within another namespace. ReSharper will take care of creating a separate file for it. For partial types, all parts will be copied even if they are located in separate files.
Change Method Signature
This refactoring allows you to modify a method signature in the following ways:
- Add parameter(s)
- Remove parameter(s)
- Rename parameter(s)
- Reorder parameter(s)
- Change return type
- Change parameter type(s)
- Rename method
Together with changing the signature, ReSharper searches for all usages of the method and modifies all calls, implementations, and overrides of the method to reflect the change. In case of newly added parameters, default values for them supplied by the user are substituted in all method calls. In overrides, the original parameter passed to the method is passed to the base call.
Extract Method
Select a block of code and invoke the Extract Method refactoring to transform it into a method (Sub or Function in VB.NET). ReSharper will automatically analyze the code to detect the return value and/or out/ref parameters.
Extract type to a new file
Using this refactoring you may swiftly move the code implementing a given type into a separate file. This refactoring can be accessed through the Move refactoring command.
Introduce Variable
Select an arbitrary expression inside the member code and invoke the Introduce Variable refactoring. A new local variable will be declared and initialized with the selected expression. The original expression will be replaced with the name of the variable. Should there be multiple occurrences of the original expression in your code, you will be given an option to replace all of them with the newly created variable. In the same manner you may also introduce constants for constant expressions.
Introduce Field
Select an expression or a local variable and apply this refactoring. It will create a new field or constant and initialize it with the expression or local variable initializer. The field can be assigned in its initializer, type constructors or current members.
Introduce Parameter
Select an expression or a local variable in a method and use this refactoring to create a new parameter from it. All call sites will be updated to reflect the changed signature, with maintaining the logic and the semantics.
Inline Variable
Select an arbitrary variable or local constant and invoke the Inline Variable refactoring. All occurrences of the selected variable in your code will be substituted with its initializer. (This refactoring does the opposite of the Introduce Variable refactoring.)
Extract Interface (C# only)
This refactoring allows you to create an interface from a class and make that class implement the created interface. You can choose the members to extract to the interface and the interface name.
Extract Superclass (C# only)
With this refactoring you can create a base class for a class and move some members to it. Just place the caret at a class declaration, select members to extract, and run the refactoring. It is very useful for moving some logic up to share it later.
Extract Class from Method Parameters (C# only)
This refactoring creates a new class or struct and converts parameters of the selected method into encapsulated fields of the newly created type (with constructor taking parameters, fields to store values and properties to retrieve values). Usages of parameters are converted to usages of properties of created type.
Make Method Static
This refactoring converts a non-static method into a static one (Shared in VB.NET) by making this a parameter of the method. After refactoring, the target method will be declared as static and necessary parameters, will be added to its call sites. The refactoring is also useful when you want to move non-static methods. In this case, use Make Method Static as a preparatory step for the Make Method Non-static refactoring.
Make Method Non-Static
This refactoring converts a static method (Shared Sub in VB.NET) to an instance method in the selected parameter's type (i.e. moves the method to parameter's type and transforms the parameter into this) and transforms method calls accordingly.
Use Base Type where Possible (C# only)
With this refactoring your code is generalized by replacing particular type usages with references to its base type or interface where possible, that is, where no members of the derived type are used. It is especially useful after the Pull Members Up refactoring.
Pull Members Up (C# only)
This refactoring allows you to move type members to a base type or interface.
Push Members Down (C# only)
This refactoring helps you to clean up your type hierarchy by moving type members to a subtype.
Safe Delete
If you are going to delete a type, type member, or any other symbol, use this refactoring to ensure that the delete operation is safe. If there are no symbol usages found, or if there are some that are safely collapsible, the symbol will be deleted. Otherwise, ReSharper will show you all the usages that are not safe to delete, allowing you to edit the corresponding code.
Convert Method to Property (C# only)
This refactoring allows users to convert non-void methods without parameters to properties with read access, and void methods with exactly one parameter to properties with write access. Paired methods can be converted to a single read/write property. This refactoring is especially useful for converting more traditional code with setter- and getter-type methods (for example, J#) to C#.
Convert Property to Method(s) (C# only)
Using this refactoring properties with read access may be converted to getter-type methods (non-void methods without parameters), and properties with write access may be converted to setter type methods (void methods with exactly one parameter). Properties allowing both read and write accesses may be converted to a pair of getter- and setter-type methods.
Convert Abstract Class to Interface (C# only)
You can apply this refactoring to an abstract class and it will be converted to interface.
Convert Interface to Abstract Class (C# only)
If you want to add some logic to interface or feel it better be a class you should invoke the refactoring and all appropriate conversions will be made. More importantly, it checks for any conflicts, i.e. for types implementing the interface that already have base type (an error in C#, which has no multiple inheritance).
Encapsulate Field
Did you forget to create an accessor property for a field? Just invoke the refactoring and it will create it and also replace usages of the field with usages of the property.
Move type to outer scope
This refactoring moves an inner type to an upper level. In case the inner type uses members of the enclosing type, a reference to the enclosing type is passed as an argument to the moved type's constructors. This refactoring can be accessed through the Move refactoring command.
Move Static Members
The refactoring moves static fields and methods to another type. If you want to move non-static methods, you can perform the following sequence of refactorings: Make Method Static-> Make Method Non-static, with the move happening in the latter.
Using Code Refactorings
To use a refactoring, you place the caret on a symbol or select a block of code and then invoke the refactoring you want from the ReSharper's Refactor menu, or by using its individual keyboard shortcut. Alternatively, the Refactor This command lets you see which refactorings can be applied at the current caret position or for the current selection; it also has a handy shortcut - Ctrl + Shift + R.
Rename Symbol
The Rename refactoring allows you to rename any symbol, including: namespaces, types, methods, parameters, local variables, properties, fields, and events. It automatically finds and corrects all references to the symbol. The Rename refactoring can be invoked directly from the editor and sometimes from other views (Class View, Object Browser).
Rename Symbol, in particular, works in all supported languages: C#, VB.NET, ASP.NET and XAML.
Move Type
You can easily move types between namespaces, with references automatically updated by ReSharper. You can also move a type to outer or nested scope or to another file.
Copy Type
It will take you just a second to create a copy of a type with a different name or within another namespace. ReSharper will take care of creating a separate file for it. For partial types, all parts will be copied even if they are located in separate files.
Change Method Signature
This refactoring allows you to modify a method signature in the following ways:
- Add parameter(s)
- Remove parameter(s)
- Rename parameter(s)
- Reorder parameter(s)
- Change return type
- Change parameter type(s)
- Rename method
Together with changing the signature, ReSharper searches for all usages of the method and modifies all calls, implementations, and overrides of the method to reflect the change. In case of newly added parameters, default values for them supplied by the user are substituted in all method calls. In overrides, the original parameter passed to the method is passed to the base call.
Extract Method
Select a block of code and invoke the Extract Method refactoring to transform it into a method (Sub or Function in VB.NET). ReSharper will automatically analyze the code to detect the return value and/or out/ref parameters.
Extract type to a new file
Using this refactoring you may swiftly move the code implementing a given type into a separate file. This refactoring can be accessed through the Move refactoring command.
Introduce Variable
Select an arbitrary expression inside the member code and invoke the Introduce Variable refactoring. A new local variable will be declared and initialized with the selected expression. The original expression will be replaced with the name of the variable. Should there be multiple occurrences of the original expression in your code, you will be given an option to replace all of them with the newly created variable. In the same manner you may also introduce constants for constant expressions.
Introduce Field
Select an expression or a local variable and apply this refactoring. It will create a new field or constant and initialize it with the expression or local variable initializer. The field can be assigned in its initializer, type constructors or current members.
Introduce Parameter
Select an expression or a local variable in a method and use this refactoring to create a new parameter from it. All call sites will be updated to reflect the changed signature, with maintaining the logic and the semantics.
Inline Variable
Select an arbitrary variable or local constant and invoke the Inline Variable refactoring. All occurrences of the selected variable in your code will be substituted with its initializer. (This refactoring does the opposite of the Introduce Variable refactoring.)
Extract Interface
This refactoring allows you to create an interface from a class and make that class implement the created interface. You can choose the members to extract to the interface and the interface name.
Extract Superclass
With this refactoring you can create a base class for a class and move some members to it. Just place the caret at a class declaration, select members to extract, and run the refactoring. It is very useful for moving some logic up to share it later.
Extract Class from Method Parameters
This refactoring creates a new class or struct and converts parameters of the selected method into encapsulated fields of the newly created type (with constructor taking parameters, fields to store values and properties to retrieve values). Usages of parameters are converted to usages of properties of created type.
Make Method Static
This refactoring converts a non-static method into a static one by making this a parameter of the method. After refactoring, the target method will be declared as static and necessary parameters, will be added to its call sites. The refactoring is also useful when you want to move non-static methods. In this case, use Make Method Static as a preparatory step for the Make Method Non-static refactoring.
Make Method Non-Static
This refactoring converts a static method to an instance method in the selected parameter's type (i.e. moves the method to parameter's type and transforms the parameter into this) and transforms method calls accordingly.
Use Base Type where Possible
With this refactoring your code is generalized by replacing particular type usages with references to its base type or interface where possible, that is, where no members of the derived type are used. It is especially useful after the Pull Members Up refactoring.
Pull Members Up
This refactoring allows you to move type members to a base type or interface.
Push Members Down
This refactoring helps you to clean up your type hierarchy by moving type members to a subtype.
Safe Delete
If you are going to delete a type, type member, or any other symbol, use this refactoring to ensure that the delete operation is safe. If there are no symbol usages found, or if there are some that are safely collapsible, the symbol will be deleted. Otherwise, ReSharper will show you all the usages that are not safe to delete, allowing you to edit the corresponding code.
Convert Method to Property
This refactoring allows users to convert non-void methods without parameters to properties with read access, and void methods with exactly one parameter to properties with write access. Paired methods can be converted to a single read/write property. This refactoring is especially useful for converting more traditional code with setter- and getter-type methods (for example, J#) to C#.
Convert Property to Method(s)
Using this refactoring properties with read access may be converted to getter-type methods (non-void methods without parameters), and properties with write access may be converted to setter type methods (void methods with exactly one parameter). Properties allowing both read and write accesses may be converted to a pair of getter- and setter-type methods.
Convert Abstract Class to Interface
You can apply this refactoring to an abstract class and it will be converted to interface.
Convert Interface to Abstract Class
If you want to add some logic to interface or feel it better be a class you should invoke the refactoring and all appropriate conversions will be made. More importantly, it checks for any conflicts, i.e. for types implementing the interface that already have base type (an error in C#, which has no multiple inheritance).
Encapsulate Field
Did you forget to create an accessor property for a field? Just invoke the refactoring and it will create it and also replace usages of the field with usages of the property.
Move type to outer scope
This refactoring moves an inner type to an upper level. In case the inner type uses members of the enclosing type, a reference to the enclosing type is passed as an argument to the moved type's constructors. This refactoring can be accessed through the Move refactoring command.
Move Static Members
The refactoring moves static fields and methods to another type. If you want to move non-static methods, you can perform the following sequence of refactorings: Make Method Static -> Make Method Non-static, with the move happening in the latter.
Using Code Refactorings
To use a refactoring, you place the caret on a symbol or select a block of code and then invoke the refactoring you want from the ReSharper's Refactor menu, or by using its individual keyboard shortcut. Alternatively, the Refactor This command lets you see which refactorings can be applied at the current caret position or for the current selection; it also has a handy shortcut - Ctrl + Shift + R.
Rename Symbol
The Rename refactoring allows you to rename any symbol, including: namespaces, types, methods, parameters, local variables, properties, fields, and events. It automatically finds and corrects all references to the symbol. The Rename refactoring can be invoked directly from the editor and sometimes from other views (Class View, Object Browser).
Rename Symbol, in particular, works in all supported languages: C#, VB.NET, ASP.NET and XAML.
Move Type
You can easily move types between namespaces, with references automatically updated by ReSharper. You can also move a type to outer or nested scope or to another file.
Copy Type
It will take you just a second to create a copy of a type with a different name or within another namespace. ReSharper will take care of creating a separate file for it. For partial types, all parts will be copied even if they are located in separate files.
Change Method Signature
This refactoring allows you to modify a method signature in the following ways:
- Add parameter(s)
- Remove parameter(s)
- Rename parameter(s)
- Reorder parameter(s)
- Change return type
- Change parameter type(s)
- Rename method
Together with changing the signature, ReSharper searches for all usages of the method and modifies all calls, implementations, and overrides of the method to reflect the change. In case of newly added parameters, default values for them supplied by the user are substituted in all method calls. In overrides, the original parameter passed to the method is passed to the base call.
Extract Method
Select a block of code and invoke the Extract Method refactoring to transform it into a method (Sub or Function in VB.NET). ReSharper will automatically analyze the code to detect the return value and/or out/ref parameters.
Extract type to a new file
Using this refactoring you may swiftly move the code implementing a given type into a separate file. This refactoring can be accessed through the Move refactoring command.
Introduce Variable
Select an arbitrary expression inside the member code and invoke the Introduce Variable refactoring. A new local variable will be declared and initialized with the selected expression. The original expression will be replaced with the name of the variable. Should there be multiple occurrences of the original expression in your code, you will be given an option to replace all of them with the newly created variable. In the same manner you may also introduce constants for constant expressions.
Introduce Field
Select an expression or a local variable and apply this refactoring. It will create a new field or constant and initialize it with the expression or local variable initializer. The field can be assigned in its initializer, type constructors or current members.
Introduce Parameter
Select an expression or a local variable in a method and use this refactoring to create a new parameter from it. All call sites will be updated to reflect the changed signature, with maintaining the logic and the semantics.
Inline Variable
Select an arbitrary variable or local constant and invoke the Inline Variable refactoring. All occurrences of the selected variable in your code will be substituted with its initializer. (This refactoring does the opposite of the Introduce Variable refactoring.)
Make Method Static
This refactoring converts a non-static method into a Shared Sub by making this a parameter of the method. After refactoring, the target method will be declared as a Shared Sub and necessary parameters, will be added to its call sites. The refactoring is also useful when you want to move non-Shared Subs. In this case, use Make Method Static as a preparatory step for the Make Method Non-static refactoring.
Make Method Non-Static
This refactoring converts a Shared Sub to an instance method in the selected parameter's type (i.e. moves the method to parameter's type and transforms the parameter into this) and transforms method calls accordingly.
Safe Delete
If you are going to delete a type, type member, or any other symbol, use this refactoring to ensure that the delete operation is safe. If there are no symbol usages found, or if there are some that are safely collapsible, the symbol will be deleted. Otherwise, ReSharper will show you all the usages that are not safe to delete, allowing you to edit the corresponding code.
Encapsulate Field
Did you forget to create an accessor property for a field? Just invoke the refactoring and it will create it and also replace usages of the field with usages of the property.
Move type to outer scope
This refactoring moves an inner type to an upper level. In case the inner type uses members of the enclosing type, a reference to the enclosing type is passed as an argument to the moved type's constructors. This refactoring can be accessed through the Move refactoring command.
Move Static Members
The refactoring moves static fields and methods to another type. If you want to move non-static methods, you can perform the following sequence of refactorings: Make Method Static -> Make Method Non-static, with the move happening in the latter.
