ReSharper 2020.2 Help

Extract Method refactoring

ReSharper | Refactor | Extract | Extract Method…

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

Consider the following example:

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

  2. Do one of the following:

    • Press Control+Alt+M.

    • Press Control+Shift+R and then choose Extract Method

    • Right-click and choose Refactor | Extract Method in the context menu.

    • Choose ReSharper | Refactor | Extract | Extract Method… in the main menu.

  3. Choose how to extract the selected statements: as a method or as a local function:

    ReSharper. Extract Method refactoring: choosing between method and local function

  4. If you choose to create a local function, specify where to place the local function inside the current function, and click Next.

  5. Type a name for the new function in the Name field.

  6. Select one of expressions that ReSharper detected as possible function 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 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.

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

  9. If you choose to create a method, select the Make static and/or Make virtual to add the corresponding modifiers to the method.

  10. If you choose to create a method, you can also specify access rights in the Access rights list.

  11. Check the resulted method signature and body 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.

ReSharper. Extract Method refactoring

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is available in Visual Basic .NETFeature is available in C++Feature is not available in HTMLFeature is not available in ASP.NETFeature is not available in RazorFeature is not available in JavaScriptFeature is not available in TypeScriptFeature is not available in CSSFeature is not available in XMLFeature is not available in XAMLFeature is not available in Resource filesFeature is not available in build script filesFeature is not available in ProtobufFeature is not available in JSON

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.

Last modified: 03 September 2020