The Introduce Field refactoring allows users to quickly create a new field based on a selected expression, initialize it with the expression, and replace occurrences of the expression in the code with references to the newly introduced field.
- Select an expression in the editor.
- On the ReSharper menu or context menu, choose Refactor | Introduce Field,
or press Ctrl+Alt+D. If more than one occurrence of the selected
expression is found, ReSharper displays the following drop-down menu:
- Choose whether to replace:
- Only the selected occurrence.
- All occurrences of the expression.
- All occurrences within the current member.
- Specify a name for the new field.
- Select an option in the Introduce field and initialize in
- Current member: initializes the field in the current member (this option is only available if you chose to replace a single occurrence or occurrences within the current member).
- Field initializer: initializes the field right where it is declared.
- Constructor(s): initializes the field in the constructor or constructors of the containing class; if necessary, creates a parameterless constructor and initializes the field from within it.
- Introduce constant: creates a constant instead of a field.
- In the Visibility group, select the visibility of the new field.
- Optionally, select Make static and Make field read-only check boxes.
- Click Next. The new field will be created and initialized according to specified settings. One or more occurrences of the selected expression will be replaced by usages of the new field.