ReSharper

Refactorings

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

Martin Fowler
www.refactoring.com

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 in the way they are defined by M. Fowler. Such local code transformations are conveniently implemented in ReSharper as context actions and other coding assistance features.

All ReSharper's refactorings work in C#, vast majority is also available in VB.NET, some in JavaScript, XAML and other supported languages.

Whenever you apply a refactoring, any affected calls in ASP.NET markup are modified accordingly. All refactorings also work within scriptlets.

Using Code Refactorings

To use a refactoring, you can place the caret:

  • On a symbol in the text editor.
  • On a code selection in the text editor.
  • On a file or a selection of files in the Solution Explorer.
  • On a type member or a selection of type members in File Structure or another ReSharper tool window.

After that, you invoke a required refactoring from the ReSharper's Refactor menu, or by using its individual keyboard shortcut. Alternatively, the Refactor This (Ctrl+Shift+R) command lets you see which refactorings can be applied at the current caret position or for the current selection.

Adjust Namespaces

This refactoring helps you synchronize namespaces to folder structure in any scope — as large as your whole solution. That is, if in a certain code file a namespace doesn't correspond to the location of the file in project structure, the namespace is renamed to comply with the file location.

All using directives referencing the modified namespaces are updated accordingly.

Change Signature

This refactoring allows you to modify a method signature in the following ways:

  • Add, remove, rename, or reorder parameter(s)
  • Change return type
  • Change parameter type(s)
  • Rename method

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

Convert Abstract Class to Interface

You can apply this refactoring to convert an abstract class to an interface. This is especially useful when you want a class to inherit from more than one abstract class, thus you need to convert one of them to an interface. The reverse functionality is also possible with the Convert Interface to Abstract Class refactoring.

Convert Anonymous to Named Type (C# only)

This refactoring converts anonymous types to named types in the scope of either the current method (locally) or the whole solution (globally). In the dialog box that this refactoring provides, you can specify whether ReSharper should generate auto-properties or properties with backing fields, and also opt to generate equalify and formatting method overloads.

Convert Extension Method to Plain Static

This dialogless refactoring converts an extension method to a static method in the same class. The reverse functionality is provided by 'Convert Static to Extension Method'.

Convert Indexer (Default Property) to Method

This refactoring works a lot like 'Convert Property to Method' but it is applied to indexers (default properties in VB.NET). You can convert only getter, only setter, or both, to a method. The reverse functionality is provided by 'Convert Method to Indexer (Default Property)'.

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

Before applying 'Convert Interface to Abstract Class'


After 'Convert Interface to Abstract Class'

Convert Method to Indexer (Default Property)

This is the reverse of 'Convert Indexer (Default Property) to Method'. It works in dialogless mode, but you may have to apply it to two methods to generate both a getter and a setter in your indexer.

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 Auto-Property (C# only)

You can now convert traditional properties with backing private fields to auto-properties that were introduced in C# 3.0.

Convert Property to Method(s)

Using this refactoring, properties with read access can be converted to getter-type methods (non-void methods without parameters), and properties with write access can be converted to setter-type methods (void methods with exactly one parameter). As to properties allowing both read and write access, you can convert them to pairs of getter- and setter-type methods.

Convert Static to Extension Method

This new refactoring converts a static method to an extension method. For the conversion to be carried out successfully, the static method must (1) have at least one argument and (2) reside in a static class. The reverse functionality is available with 'Convert Extension Method to Plain Static'.

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.

Encapsulate Field

The Encapsulate Field refactoring allows you to quickly create an accessor property from an existing field. Usages of the field are automatically replaced with usages of the property. An apparent advantage of this refactoring is that you can disallow direct access to a field through the use of properties.

Extract Class

Do you have a class doing work that should be really done by two different classes? Use this refactoring to decompose a complex class into two single-responsibility classes. Extract Class will help you choose methods and fields to move from the old class to the new class. It will also warn of any broken dependencies and accessibility issues and suggest conflict resolution options.

Extract Class from 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.

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 specify a name for the interface.

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

Inline Field

When a field has a single write usage, this refactoring substitutes read usages of the field with its initialization expression, deleting the field declaration and the write usage along the way.

Inline Method

Whenever possible, this refactoring is able to transfer a method's body into the body of its callers and remove the method altogether. The reverse functionality is provided by 'Extract Method'.

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. The reverse functionality is provided by 'Introduce Variable'.

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, while maintaining the logic and the semantics.

If an expression uses local variables that are inaccessible at a call site, this refactoring allows to pass them as delegates.

Introduce Variable

Select an arbitrary expression inside member code and invoke the Introduce Variable refactoring. A new implicitly or explicitly typed 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.

Before applying 'Introduce Variable'

Introduce Variable
Introduce Variable

After applying 'Introduce Variable'

Introduce Variable
Introduce Variable

Make Method Non-Static/Non-Shared

This refactoring converts a static method (Shared 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.

Make Method Static/Shared

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 (Shared in VB.NET), and necessary parameters will be added to its call sites. The refactoring is also useful when you want to move non-static methods (non-Shared in VB.NET). In this case, use Make Method Static as a preparatory step for the Make Method Non-static refactoring.

Move Static Member

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.

Move String to Resource

A part of ReSharper's internationalization feature pack, this refactoring helps extract localizable strings into resource files. During the refactoring, explicit string usages are replaced with references to resource wrappers.

Move to Folder

This refactoring helps move a class, a file, or a selection of classes or files to another project or folder in solution. When executing the move, ReSharper updates using directives if necessary. Optionally, it can rename namespaces according to the new location and distribute classes being moved to separate files.

Move Type to Another File or Namespace

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.
Using this refactoring, you can swiftly move the code implementing a given type into a separate file. This refactoring can be accessed through the Move refactoring command.

When you use XAML and you move a .NET class to another namespace, XAML markup references to the class are updated, and XAML import directives are inserted/changed accordingly. This refactoring can also be invoked right from XAML markup on any type reference.

Move Type to Outer Scope

This refactoring moves an inner type to an upper level. In case that 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 Types into Matching Files

This refactoring can be applied to a single file or a selection of files that have multiple types each. ReSharper creates dedicated files for each of these types are moves them there.

'Move Types into Matching Files' is an indispensable helper if you prefer to first use types, then declare them with ReSharper's Create from Usage feature.

There's also a shortcut for this refactoring that is available when you press Alt+Enter on the name of a class that doesn't match the name of the file it's in:

Pull Members Up

This refactoring helps you move type members to a superclass or an interface. This operation is useful as a way to generalize behavior. ReSharper analyzes all members in the current class and makes a list of members that you can pull up. Before completing the refactoring, ReSharper also checks for possible conflicts, for example, whether the members that you are trying to move to a superclass will be accessible in the destination type. The reverse functionality is available with 'Push Members Down'.

Push Members Down

This refactoring helps you clean up your type hierarchy by moving type members to a subtype. This operation is useful as a way to specialize behavior. ReSharper analyzes all members of the selected type and makes a list of members that you can push down. Before completing the refactoring, ReSharper also checks for possible conflicts, for example, whether the members you are trying to push down will be accessible in the destination type. The reverse functionality is available with 'Push Members Up'.

Rename

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 works with all supported languages and technologies, including C#, VB.NET, JavaScript, ASP.NET, JavaScript, XML, XAML, and build scripts.

Automatic reference correction fully extends to XAML markup when you use the Rename refactoring. In addition, specific XAML symbols such as namespace aliases and resources can be renamed easily:

Rename Refactoring in XAML

For build scripts, when you rename a property or target with ReSharper, all its references and even usages in comments and strings are automatically updated to reflect the change.

Replace Constructor with Factory Method

This refactoring encapsulates a constructor with a static method that returns a new instance of a class.

The Factory Method pattern is a way of creating objects without specifying the exact class of object that will be created. ReSharper generates a separate method for creating objects. Subclasses can override it to specify the derived type of object that will be created.

Safe Delete

If you are going to delete a type, type member, or any other symbol in C# or VB.NET code files, ASP.NET code and markup, build scripts, or XAML, 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.

Starting from ReSharper 5, this refactoring can be applied to a selection of files or file members, enabling you to safely delete entire subsystems.

When you invoke Safe Delete on a target or a property in a build file, ReSharper does all necessary validations and operations to remove selected symbol. ReSharper can also remove any conflicting references for you.

Transform Out Parameters to Tuple

This refactoring doesn't do much more than its name implies: if you prefer returning a Tuple instead of using out parameters, use this refactoring that is available with the caret on an out parameter in C# or a ByRef parameter in VB.NET.

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.

Note:

All keyboard shortcuts provided in the "Features" section are taken from ReSharper 2.x / IDEA keyboard scheme. For details on ReSharper's two default keymaps, see Documentation and Demos.