Skip to content

EqualityChecking

tom-englert edited this page Feb 8, 2025 · 5 revisions

Equality check will be done before setting the field and firing the event.

public string Property1
{
    get
    {
        return property1;
    }
    set
    {
        if (String.Equals(property1, value))
            return;  

        property1 = value;
        OnPropertyChanged("Property1");
    }
}

The method used for equality checking is based on the property Type. The evaluation is done in the following order.

  • Check if the Type is a Nullable<T> and use Nullable.Equals<T>(T?,T?).
  • Check if the Type has a static Equals method with two parameters matching Type.
  • Check if the Type has and Equality Operator (==) with both parameters matching Type.
  • Use Object.Equals(object, object).

If you don't want to perform equality checking for a give property then add a [DoNotCheckEquality].

Note

Equality checking will be done at the entry point of the setter and not only bypass the setting of the backing field and the call to OnPropertyChanged, but also any custom code in the setter, including code injected by other weavers like PropertyChanging.Fody.

If you need to run custom code in the setter despite the outcome of the equality check, you can either disable the equality check, or mark the property with [DoNotNotify] and implement the setter by your own.

Clone this wiki locally