c#语言中的事件处理程序

Pya*_*dav 2 c# programming-languages c#-4.0

class Plane 
{
    public event EventHandler Land;

    protected void OnLand()
    {
        if ( null != Land ) 
        {
            Land( this, null );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它是事件处理程序的最佳实践:

EventHandler temp = Land;
if ( null != temp ) 
{
    temp( this, null );
}
Run Code Online (Sandbox Code Playgroud)

这真的有必要吗?在什么情况下可能会与Land不同?

Eri*_*ich 6

在多线程访问的情况下,我相信.如果你没有缓存引用,另一个线程可以在你的后卫之后但在你开火之前将其清空.