Back to Devexpress

Add Getter/Setter

coderushforroslyn-115364-coding-assistance-code-providers-add-getter-setter.md

latest1.7 KB
Original Source

Add Getter/Setter

  • Aug 03, 2020
  • 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.

  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.

csharp
public int A {
    get {
        return a;
    }
    set {
        a = value;
    }
}
vb
Public Property A As Integer
    Get
        Return _a
    End Get
    Set(value As Integer)
        _a = value
    End Set
End Property

See Also

Convert to Auto-Implemented Property

Collapse/Expand Getter/Setter