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
  • 12,913
  • 8
  • 60
  • 81
  • 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

4

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.
  • 142,212
  • 27
  • 297
  • 366