Inline Variable refactoring
ReSharper | Refactor | Inline | Inline…
Ctrl0R,0I (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.
Before refactoring
static string ReversedString(string input){ var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); return reversed;}
After refactoring
static string ReversedString(string input){ var chars = input.ToCharArray(); Array.Reverse(chars); return new string(chars);}
tip
The reverse functionality is available with the Introduce Variable refactoring refactoring.
Place the caret at the declaration or a usage of a variable in the editor.
Do one of the following:
Press Ctrl0R,0I and then choose Inline Variable
Press CtrlShift0R and then choose Inline Variable.
Right-click and choose Refactor | Inline Variable from the context menu.
Choose ReSharper | Refactor | Inline | Inline… from the main menu.
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 Languages and frameworks section.