Trigger Events; Safe/Unsafe

Hi guys, please consider the following two code snippets.

 Company _company; public Company company { get { return _company; } set { if (value != _company) { company = value; OnCompanyChanged(); } } } public delegate void CompanyChangedEventHandler(); public event CompanyChangedEventHandler CompanyChanged; protected void OnCompanyChanged() { CompanyChangedEventHandler handler = CompanyChanged; if (handler != null) handler(); } 


 public Company company { get { return _company; } set { if (value != _company) { company = value; CompanyChanged(); } } } 

The only difference between the two is that one uses OnCompanyChanged, which then creates the event handler and then fires it, whereas the other just fires the event directly.

I'm working in WPF, working with very simple data structures and the WPF UI. As far as I know, everything I have right now is working synchronously. Should I care about one way as opposed to another?

Should I use method 1, just out of principle, or best practice? Its just extra code I could remove if it isn't necessary.

Thanks, any insight would be appreciated.

by Failosipher via /r/csharp

Leave a Reply