Encapsulate Field refactoring
This refactoring allows you to create an auto-property or a property with the backing field from an existing field. Usages of the field are updated automatically.
In the example below, a field is replaced with an auto-property:
|Before refactoring||After refactoring|
| || |
To encapsulate a field
- Place the caret at the declaration or a usage of a field in the editor, or select it in the File Structure window.
- Do one of the following:
- Press Ctrl+R,E.
- Press Ctrl+Shift+R and then choose Encapsulate Field
- Right-click and choose Refactor | Encapsulate Field in the context menu.
- Choose in the main menu.
- ReSharper suggests a name for the property based on the field name and the naming style for properties. Modify the name if necessary.
- Specify whether to create an auto-property or create a normal property and use the field as its backing field.
- By default, ReSharper replaces all (local and external) usages of the field with the property. If you are creating a normal property, you can clear the Do not update local usages check box to leave references to the field in the current type.
- Specify the property visibility.
- If you are creating a normal property, you can tick the Make field private check box to disallow direct access to the field.
- To apply the refactoring, click Next.
- 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/technologies:
The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.