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:
To move an instance method
- Place the caret at the declaration or a usage of an instance method in the editor, or select it in the Structure window.
- Do one of the following: Move Instance Method dialog will open.
- Select the destination type.
- To apply the refactoring, click Next.
- If no conflicts are found, Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.