1

In WPF, is it a best practice to check if the value of a model's property will actually be changed in a property setter, before calling NotifyPropertyChanged?

For example:

    private string foobar;

    public string Foobar
    {
        get
        {
            return this.foobar;
        }

        set
        {
            if (value != this.foobar)
            {
                this.foobar = value;
                this.NotifyPropertyChanged("Foobar");
            }
        }
    }

The alternative is to not check and just call NotifyPropertyChanged every time:

    private string foobar;

    public string Foobar
    {
        get
        {
            return this.foobar;
        }

        set
        {
            this.foobar = value;
            this.NotifyPropertyChanged("Foobar");
        }
    }

I've seen both styles used in code samples. What, if any, are the advantages and disadvantages of each approach?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
  • I've just seen that the same question has already been asked: http://stackoverflow.com/questions/2623697/should-a-setter-return-immediately-if-assigned-the-same-value – Luke Girvin May 20 '13 at 13:39

1 Answers1

5

I always do the check because the event seems to imply that an actual change should have happened.

(Also it prevents unnecessary updates)

H.B.
  • 166,899
  • 29
  • 327
  • 400