Inline Variable refactoring
Ctrl+R, I
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 | After refactoring |
---|---|
public static string ReversedString(string input)
{
var chars = input.ToCharArray();
Array.Reverse(chars);
var reversed = new string(chars);
return reversed;
} | public static string ReversedString(string input)
{
var chars = input.ToCharArray();
Array.Reverse(chars);
return new string(chars);
} |
To inline a variable
- Place the caret at the declaration or a usage of a variable in the editor.
- Do one of the following:
- Press Ctrl+R, I and then choose Inline Variable
- Press Ctrl+Shift+R and then choose Inline Variable
- Choose in the main menu.
- If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
Last modified: 27 December 2017