This is how I always wrote event raiders; e.g. PropertyChanged:
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string name)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
In the latest Visual Studio, however, the thingamabob light bulb suggested simplifying the code:
private void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
Although I always support simplification, I wanted to make sure it was safe. In my source code, I assign a variable handler to prevent a race condition in which a subscriber can be removed between a zero check and a call. It seems to me that the new simplified form will suffer from this condition, but I would like to know if anyone can confirm or deny it.
source
share