ReSharper 2020.2 Help

Inline Variable refactoring

ReSharper | Refactor | Inline | Inline…
Control+Alt+N
ReSharper_InlineVariable

This refactoring allows you to replace all occurrences of a variable in the code with its initializer. Note that the refactoring should be only applied if the variable value stays unchanged after initialization.

In the example below, we use this refactoring to inline the reversed variable.

static string ReversedString(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); return reversed; }
static string ReversedString(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); return new string(chars); }

Inline a variable

  1. Place the caret at the declaration or a usage of a variable in the editor.

  2. Do one of the following:

    • Press Control+Alt+N and then choose Inline Variable

    • Press Control+Shift+R and then choose Inline Variable

    • Right-click and choose Refactor | Inline Variable in the context menu.

    • Choose ReSharper | Refactor | Inline | Inline… in the main menu.

  3. 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.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is not 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 available in JavaScriptFeature is 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