Skip to main content

Add Getter/Setter

  • 2 minutes to read

Purpose

This Code Provider is used to add a get or set accessor to a property which does not have one.

Note

Add Getter/Setter is a cascading Code Provider. This means that the Code Provider affects all descendants implementing the property and ancestors from which the property was inherited.

Availability

Available when the caret is located within the property block.

Usage

  1. Place the caret inside the property block as shown in the code below.

    Note

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

    public int A {
        set {
            a = value;
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Add Getter or Add Setter from the menu.

After execution, the getter/setter is added to the property. If the property already contains the opposite accessor, the Code Provider will try to guess the private variable name linked with the property and populate the getter body with the return statement or setter body with the assignment of the corresponding local variable. Otherwise, the generated getter/setter will throw the NotImplementedException exception.

public int A {
    get {
        return a;
    }
    set {
        a = value;
    }
}
See Also