Skip to main content
A newer version of this page is available. .

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.

NOTE

This Refactoring is opposite to Convert to Property with Backing Field.

Availability

Available when the caret is on a property declaration statement. The property should have a getter that does nothing but return a field value and a setter that only assigns that field a value.

Usage

  1. Place the caret on 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. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Convert to Auto-implemented Property (Convert to Auto-implemented Property (convert all) to convert all properties in the active type) from the menu.

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

public string A { get; set; }
NOTE

This feature is available as a part of Code Cleanup.

See Also