The Move Instance Method 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.
This refactoring may be helpful if you see that the logic of a method suits better for the type that is passed as the method parameter or a field rather than for the current type.
Consider the following example:
The method StartTheCar clearly belongs to the Car class. So, ReSharper helps us to move this method exactly there:
Note that the refactoring would do the same if the Car were used as a filed in the Person type.
- Place the caret at the name of the method that you want to move.
Do one of the following:
- On the main menu, choose ReSharper| Refactor | Move Instance Method.
- Choose Refactor | Move Instance Method on the context menu.
- Select the destination type if there are multiple suggestions.
- Optionally, change the access modifier.
- Click Next.
- If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise,resolve conflicts.
This feature is supported in the following languages/technologies:
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.