JetBrains Rider 2017.2 Help

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 refactoringAfter refactoring
private string myString; public void DoSomething(string input) { myString = input; }
public string MyString { get; private set; } public void DoSomething(string input) { MyString = input; }

To encapsulate a field

  1. Place the caret at the declaration or a usage of a field in the editor, or select it in the Structure window.
  2. Do one of the following:
    • Press Ctrl+Shift+R and then choose Encapsulate Field
    • Choose Refactor | Encapsulate Field in the main menu.
    The Encapsulate Field dialog will open.
  3. JetBrains Rider suggests a name for the property based on the field name and the naming style for properties. Modify the name if necessary.
  4. Specify whether to create an auto-property or create a normal property and use the field as its backing field.
  5. By default, JetBrains Rider 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.
  6. Specify the property visibility.
  7. If you are creating a normal property, you can tick the Make field private check box to disallow direct access to the field.
  8. To apply the refactoring, click Next.
  9. If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
JetBrains Rider. Encapsulate Filed refactoring
Last modified: 17 November 2017

See Also