ReSharper 2022.1 Help

Move Instance Method refactoring

This refactoring allows you to move an instance (non-static) method to another type. In contrast to moving static members, instance method cannot be moved to just any type. The list of potential target types includes types of the method parameters and types of fields in the current type. If the method uses other class members, the refactoring will pass the source class as a parameter. It will also change access rights of non-public members and encapsulate fields into public properties, if necessary. All usages of the method are updated automatically.

This refactoring may be helpful if you see that the logic of a method fits better into another type, which is passed as the method parameter or a field.

In the example below, we apply the refactoring to the LogDrawing instance method to move it to the Logger class. The private pivot field, which is used in the method is automatically encapsulated into the corresponding property:

class Shape { private Point pivot; private void LogDrawing(Logger logger) { var msg = $"Shape is drawn at {pivot.X}, {pivot.Y}"; logger.Log(msg); } } class Logger { public void Log(string msg) { // log the message } }
class Shape { private Point pivot; public Point Pivot { set { pivot = value; } get { return pivot; } } } class Logger { public void Log(string msg) { // log the message } private void LogDrawing(Shape shape) { var msg = $"Shape is drawn at" {shape.Pivot.X}, {shape.Pivot.Y}"; this.Log(msg); } }

Move an instance method to another type

  1. Place the caret at the declaration or a usage of an instance method in the editor, or select it in the File Structure window.

  2. Do one of the following:

    • Press F6.

    • Press Control+Shift+R and then choose Move Instance Method

    • Right-click and choose Refactor | Move Instance Method in the context menu.

    • Choose ReSharper | Refactor | Move… in the main menu.

    The Move Instance Method dialog will open.

  3. Select the destination type.

  4. To apply the refactoring, click Next.

  5. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

ReSharper. Move Instance Method refactoring

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic .NET

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by language section.

Last modified: 08 March 2021