ReSharper

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

All ReSharper's refactorings support VB8, except for those specifically marked as C#-only.

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 place the caret on a symbol or select a block of code and then invoke a required refactoring 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.

MoveType
MoveType

to the top

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.

to the top

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.

to the top

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 equality and formatting method overloads.

to the top

Convert Extension Method to Plain Static (C# only)

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

to the top

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

to the top

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

to the top

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.

to the top

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

to the top

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.

to the top

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.

to the top

Convert Static to Extension Method (C# only)

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

to the top

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.

to the top

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.

to the top

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.

to the top

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.

to the top

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.

to the top

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.

to the top

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

to the top

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

to the top

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.

to the top

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.

to the top

Introduce Variable

Select an arbitrary expression inside the 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 'Introduce Variable'

Introduce Variable
Introduce Variable

After 'Introduce Variable'

Introduce Variable
Introduce Variable

to the top

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.

to the top

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.

to the top

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.

to the top

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.

to the top

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.

to the top

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

to the top

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

to the top

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 - namely, C#, VB.NET, ASP.NET, 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.

to the top

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.

to the top

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.

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.

to the top

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.

to the top