开发者

Which of these is the more correct implementation of INotifyPropertyChanged?

I've wondered often about this: Which of the following two implementations of INotifyPropertyChanged for any property Foo is the better, or even more correct one?

/* version A: */                      |     /* version B: */
private bool foo;                     |     private bool foo;
public bool Foo                       |     public bool Foo
{                                     |     {
   set                                |        set
   {                                  |        {
                                      |           if (value != foo)
                                      |           {
      foo = value;                    |   开发者_如何转开发           foo = value;
      OnPropertyChanged("Foo");       |              OnPropertyChanged("Foo");
                                      |           }
    }                                 |        }
}                                     |     }

(I've omitted everything that is not relevant for this question.)

I suspect it should be version B on the right, as it prevents unnecessary events from being triggered. However, are there situations where these very same, missed events could lead to problems?


Version B looks more correct as it doesn't raise the event if the property hasn't actually changed value. There won't be any problems with Version A, it's just an unnecessary call which won't update any of the UI.

And as the documentation states:

Notifies clients that a property value has changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜