Introduce Variable refactoring
This refactoring allows you to create a new local variable or constant based on a selected expression, initialize it with the expression, and finally replace all occurrences of the expression in the method with references to the newly introduced variable.
In the example below, we use this refactoring to replace two occurrences of the same string with a variable:
Replace one or more occurrences of an expression with a variable
Select an expression in the editor.
Do one of the following:
Press Control+Alt+V.
Press Control+Shift+R and then choose Introduce Variable.
Right-click and choose Refactor | Introduce Variable from the context menu.
Choose
from the main menu.
If more than one occurrence of the selected expression is found, ReSharper displays the drop-down menu where you can choose whether to apply the refactoring to all occurrences or only to the current one.
ReSharper adds the new local variable and suggests choosing its type and name. You can choose one of the suggestions or type your own values. Press Tab or Enter to move to the next input position. Shift+Tab to return to the previous input position.
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 ReSharper by language section.