Josh Smith's EventHandler Question from MVVM Sample Application

The following code is from Josh Smith's MVVM example:

/// <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);
 }

Commented lines are my complement. My question is that commented out lines will do the same as without digging? So why create another EventHandler link? Or am I missing something? Thanks

+3
source share
3 answers

Tanmoy is right. This is to prevent the possibility of changing RequestClose (for example, null) in another thread after your "if", but before your "RequestClose ()".

+4
source

- . .

!

+1

RequestClose null , , , . , . , .

+1

Source: https://habr.com/ru/post/1765749/


All Articles