在實現其最基本的形式INotifyPropertyChanged
接口,大多數人似乎實現它這樣的:INotifyProperyChanged - 爲什麼要額外分配?
public virtual void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我的問題是:爲什麼的var propertyChanged = PropertyChanged;
額外分配?這只是一個偏好問題,還是有充分的理由?當然以下是有效的?
public virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
http://stackoverflow.com/questions/786383/c-sharp-events-and-thread-safety – 2012-04-03 06:59:53