ReSharper 2024.3 Help

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.

static void PrintReversed(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); Console.WriteLine(reversed); }
static void PrintReversed(string input) { var reversed = ReverseStr(input); Console.WriteLine(reversed); } private static string ReverseStr(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); return reversed; }

Extract a method from selected statements

  1. In the editor, select one or more statements that you want to convert into a local function.

  2. 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.

  3. In the popup that opens, select Extract Local Function.

  4. A dialog appears where you can configure the extracted local function:

    ReSharper. Extract Method refactoring
  5. Type a name for the new local function in the Name field.

  6. 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.

  7. 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.

  8. To change the order of the parameters, select a parameter in the list and use the Move Up and Move Up buttons.

  9. Select the Make static and/or Make virtual to add the corresponding modifiers to the method.

  10. If necessary, change the visibility of the method in the Visibility list.

  11. Check the signature and body of the resulting local function in the Preview field.

  12. To apply the refactoring, click Next.

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

Extract local function from selected statements

  1. In the editor, select one or more statements that you want to convert into a local function.

  2. 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.

  3. In the popup that opens, select Extract Local Function.

  4. ReSharper will show you a preview of the signature and location of the new function.

    ReSharper: Extract local function refactoring

    Use the Up and Down keyboard keys to change the desired location of the function, and then press Enter.

  5. A dialog appears where you can configure the extracted local function:

    ReSharper. Extract Method refactoring
  6. Type a name for the new local function in the Name field.

  7. 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.

  8. 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.

  9. To change the order of the parameters, select a parameter in the list and use the Move Up and Move Up buttons.

  10. Check the signature and body of the resulting local function in the Preview field.

  11. To apply the refactoring, click Next.

  12. 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:

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

Extract method 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 more information about other languages, refer to corresponding topics in the Languages and frameworks section.

Last modified: 23 September 2024