Skip to main content

Introduce Setter Guard Clause

Purpose

Introduces a value changed check at the beginning of a property setter. This will prevent the property value from being overwritten by a similar one, which may speed up your code.

Availability

Available when the caret is on a set keyword.

Usage

  1. Place the caret on a set keyword.

    Note

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

    public static double CurrencyRateEUR {
        get {
            return currencyRateEUR;
        }
        set {
            currencyRateEUR = value;
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Introduce Setter Guard Clause from the menu.

After execution, the Code Provider adds the guard clause to the beginning of the setter body.

public static double CurrencyRateEUR {
    get {
        return currencyRateEUR;
    }
    set {
        if (currencyRateEUR == value)
            return;
        currencyRateEUR = value;
    }
}