Inline Variable refactoring
Refactor | Inline | Inline...
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
static string ReversedString(string input){ var chars = input.ToCharArray(); Array.Reverse(chars); var reversed = new string(chars); return reversed;}
After
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 and then choose Inline Variable
Press and then choose Inline Variable.
Choose Refactor | Inline Variable from the main menu.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action to roll back all these changes with a single keystroke.