Compartilhar via


Encapsulate Field Refactoring (C#)

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The Encapsulate Field refactoring operation enables you to quickly create a property from an existing field, and then seamlessly update your code with references to the new property.

When a field is public, other objects have direct access to that field and can modify it, undetected by the object that owns that field. By using properties to encapsulate that field, you can disallow direct access to fields.

To create the new property, the Encapsulate Field operation changes the access modifier for the field that you want to encapsulate to private, and then generates get and set accessors for that field. In some cases, only a get accessor is generated, such as when the field is declared read-only.

The refactoring engine updates your code with references to the new property in the areas specified in the Update References section of the Encapsulate Field dialog box.

To create a property from a field

  1. Create a console application named EncapsulateFieldExample, and then replace Program with the following example code.

    class Square
    {
        // Select the word 'width' and then use Encapsulate Field.
        public int width, height;
    }
    class MainClass
    {
        public static void Main()
        {
            Square mySquare = new Square();
            mySquare.width = 110;
            mySquare.height = 150;
            // Output values for width and height.
            Console.WriteLine("width = {0}", mySquare.width);
            Console.WriteLine("height = {0}", mySquare.height);
        }
    }
    
  2. In the Code Editor, place the cursor in the declaration, on the name of the field that you want to encapsulate. In the example below, place the cursor on the word width:

    public int width, height;
    
  3. On the Refactor menu, click Encapsulate Field.

    The Encapsulate Field dialog box appears.

    You can also type the keyboard shortcut CTRL+R, E to display the Encapsulate Field dialog box.

    You can also right-click the cursor, point to Refactor, and then click Encapsulate Field to display the Encapsulate Field dialog box.

  4. Specify settings.

  5. Press ENTER, or click the OK button.

  6. If you selected the Preview reference changes option, then the Preview Reference Changes window opens. Click the Apply button.

    The following get and set accessor code is displayed in your source file:

    public int Width
    {
        get { return width; }
        set { width = value; }
    }
    

    The code in the Main method is also updated to the new Width property name.

    Square mySquare = new Square();
    mySquare.Width = 110;
    mySquare.height = 150;
    // Output values for width and height.
    Console.WriteLine("width = {0}", mySquare.Width);
    

Remarks

The Encapsulate Field operation is only possible when the cursor is positioned on the same line as the field declaration.

For declarations that declare multiple fields, Encapsulate Field uses the comma as a boundary between fields, and initiates refactoring on the field that is nearest the cursor and on the same line as the cursor. You can also specify which field you want to encapsulate by selecting the name of that field in the declaration.

The code that is generated by this refactoring operation is modeled by the encapsulate field code snippets feature. Code Snippets are modifiable. For more information, see Code Snippets.

See Also

Refactoring (C#) Visual C# Code Snippets