Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question on EventHandler from Josh Smith's MVVM sample application

The following code is from the MVVM sample by Josh Smith:

/// <summary>
/// Raised when this workspace should be removed from the UI.
/// </summary>
public event EventHandler RequestClose;

void OnRequestClose()
{
    //if (RequestClose != null)
    //        RequestClose(this, EventArgs.Empty);
    EventHandler handler = this.RequestClose;
    if (handler != null)
        handler(this, EventArgs.Empty);
 }

The commented lines are my addition. My question is the commented lines would do the same thing as the uncommented lines right? So why create another EventHandler reference? Or am I missing something here? Thanks

like image 306
Aishwar Avatar asked Mar 18 '26 21:03

Aishwar


2 Answers

Tanmoy is right. This is done to prevent possibility of RequestClose being changed (to null, for example) in other thread after your "if" but before your "RequestClose()".

like image 179
Dima Stefantsov Avatar answered Mar 20 '26 16:03

Dima Stefantsov


It makes no difference - you are acting on the same event reference in both cases. I prefer your commented code.

Enjoy!

like image 23
Doug Avatar answered Mar 20 '26 16:03

Doug