Extract Method refactoring
This refactoring allows you to create a new method or a local function based on the selected code fragment. ReSharper analyzes the selected statements and detects variables that can be converted into method parameters or represent its return value.
Consider the following example. The method PrintReversed()
actually does two things: it reverses the string and prints it. We can select the reversing logic, which is all statements except Console.WriteLine(reversed);
, and use this refactoring to move that logic to a new PrintReversed()
method.
Extract a method from selected statements
In the editor, select one or more statements that you want to convert into a local function.
Do one of the following:
Press Control+Alt+M.
Press Alt+Enter and choose Extract Method.
Press Control+Shift+R and then choose Extract Method.
In the popup that opens, select Extract Local Function.
A dialog appears where you can configure the extracted local function:
Type a name for the new local function in the Name field.
Select one of expressions that ReSharper detected as possible return values in the Return list. There may be other expressions detected as possible return values. They are listed as
out
parameters.Include or exclude parameters using the corresponding checkboxes in the Parameters area. If you exclude a parameter from the list, the local variable with the same name and type will be created if necessary.
To change the order of the parameters, select a parameter in the list and use the Move Up and Move Up buttons.
Select the Make static and/or Make virtual to add the corresponding modifiers to the method.
If necessary, change the visibility of the method in the Visibility list.
Check the signature and body of the resulting local function in the Preview field.
To apply the refactoring, click Next.
If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
Extract local function from selected statements
In the editor, select one or more statements that you want to convert into a local function.
Do one of the following:
Press Control+Alt+M.
Press Alt+Enter and choose Extract Method.
Press Control+Shift+R and then choose Extract Method.
In the popup that opens, select Extract Local Function.
ReSharper will show you a preview of the signature and location of the new function.
Use the Up and Down keyboard keys to change the desired location of the function, and then press Enter.
A dialog appears where you can configure the extracted local function:
Type a name for the new local function in the Name field.
Select one of expressions that ReSharper detected as possible return values in the Return list. There may be other expressions detected as possible return values. They are listed as
out
parameters.Include or exclude parameters using the corresponding checkboxes in the Parameters area. If you exclude a parameter from the list, the local variable with the same name and type will be created if necessary.
To change the order of the parameters, select a parameter in the list and use the Move Up and Move Up buttons.
Check the signature and body of the resulting local function in the Preview field.
To apply the refactoring, click Next.
If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
This feature is supported in the following languages and technologies:
The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.