ReSharper 2024.3 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:

private string myString; public void DoSomething(string input) { myString = input; }
public string MyString { get; private set; } public void DoSomething(string input) { MyString = input; }

Create a new property to expose or replace the field

  1. Place the caret at the declaration or a usage of a field in the editor, or select it in the File Structure window window.

  2. Do one of the following:

    • Press Control+R E.

    • Press Control+Shift+R and then choose Encapsulate Field.

    • Right-click and choose Refactor | Encapsulate Field from the context menu.

    • Choose ReSharper | Refactor | Encapsulate Field… from the main menu.

    The Encapsulate Field dialog will open.

  3. ReSharper suggests a name for the property based on the field name and the naming style for properties. Modify the name if necessary.

  4. Specify visibility for the created property.

  5. If you choose to create an auto-property, the field will be removed and its usages replaced with the property everywhere.

    Otherwise, the field will be used as the backing field for the new property and you will be able to choose how to update the property and its usages:

    • Select Read usages/Write usages to replace the corresponding usages of the filed with the property.

    • Select Name usages to replace occurrences of the field name inside nameof() expressions and XML documentation comments with the name of the property.

    • Select Do not update local usages to leave references to the field in the current type.

    • If the field was not private, select Make field private to disallow direct access to the field outside the current type.

  6. To apply the refactoring, click Next.

  7. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

ReSharper. Encapsulate Field refactoring

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

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.

Last modified: 25 September 2024