- 2 minutes to read
Wraps a selected object's properties in new properties declared in the active type. This Refactoring is helpful when you need to expose a protected or private object's properties that are declared as a current type's field.
Available when the cursor is in a field whose type exposes one or more public properties.
Place the caret in a field.
The blinking cursor shows the caret's position at which the Refactoring is available.
- Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
- Select the Encapsulate Property from the menu.
- Select the properties to encapsulate using the mouse or Space key.
- Hit Enter to confirm the selection.
After execution, the Refactoring adds properties to the active type. The added properties give access to the selected field's properties. After adding properties, this refactoring updates all references so that they use the newly generated properties.
The following code is generated in the Program class if you selected all the available properties (Age and FullName):