Rider Help

Extract Method refactoring

Ctrl+R, M

This refactoring allows you to create a new method based on a selected code fragment. Rider analyses the selected statements and detects variables that can be converted into method parameters or represent its return value.

Consider the following example:

Before refactoringAfter refactoring
public static void PrintReversedString(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); Console.WriteLine(reversed); }
public static void PrintReversedString(string input) { var reversed = ReverseString(input); Console.WriteLine(reversed); } private static string ReverseString(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); return reversed; }

To extract a method from a code block

  1. In the editor, select one or more statements that you want to convert into a method.
  2. Do one of the following:
    • Press Ctrl+R, M.
    • Press Ctrl+Shift+R and then choose Extract Method
    • Choose Refactor | Extract Method in the main menu.
    The Extract Method dialog will open.
  3. Type a name for the new method in the Name field.
  4. Select one of expressions that Rider detected as possible method return values in the Return drop-down list. There may be other expressions detected as possible return values. They are listed as out parameters.
  5. Include or exclude parameters using the corresponding check boxes in the Parameters area. If you exclude a parameter from the list, the local variable with the same name and type will be created in the method, if necessary.
  6. Select the Make static and/or Make virtual to add the corresponding modifiers to the method.
  7. Specify access rights in the Access rights drop-down list.
  8. To change order of parameters, select parameters in the list and use Move Up and Move Up buttons below the parameters list.
  9. Check the resulted method signature and body in the Preview field.
  10. To apply the refactoring, click Next.
  11. If no conflicts are found, Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
Rider. Extract Method refactoring
Last modified: 11 October 2017

See Also