Back to Devexpress

Introduce Setter Guard Clause

coderushforroslyn-115704-coding-assistance-code-providers-introduce-setter-guard-clause.md

latest1.1 KB
Original Source

Introduce Setter Guard Clause

  • Aug 03, 2020

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.

  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.

csharp
public static double CurrencyRateEUR {
    get {
        return currencyRateEUR;
    }
    set {
        if (currencyRateEUR == value)
            return;
        currencyRateEUR = value;
    }
}
vb
Public Property CurrencyRateEUR() As Double
    Get
        Return CurrencyRateEUR
    End Get
    Set(ByVal value As Double)
        If CurrencyRateEUR = value Then
            Return
        End If
        CurrencyRateEUR = value
    End Set
End Property