Skip to main content

Convert to Auto-Implemented Property

  • 2 minutes to read

#Purpose

Converts a property with fully described accessors to an auto-implemented property. This removes the redundancy and makes the code clearer.

This refactoring is the opposite of the Convert to Property with Backing Field refactoring.

#Availability

This refactoring is available when the caret is in a property declaration statement. A property should have a getter that does nothing but return a field’s value, and a setter that assigns a value to this field.

#Usage

  1. Place the caret in a property declaration statement.

    Note

    The blinking cursor shows the caret’s position at which the Refactoring is available.

    private string a;
    public string A {
        get { return a; }
        set { a = value; }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Convert to Auto-Implemented Property from the menu.

    CRR_CodeCleanUp

    Note

    You can also use the Convert All Properties to Auto-Implemented refactoring to convert all properties in the active type.

After execution, this refactoring removes the intermediate private variable along with the getter and setter bodies.

public string A { get; set; }

CodeRush can convert not-implemented properties to auto-implemented properties. For more information, refer to the following topic: Code Actions Settings.

You can also apply the “Make properties auto-implemented” rule in code cleanup. See the following topic for more information: Configure Cleanup.

See Also