I noticed that quite a lot of code uses the following code snippet to call an event handler.
Public event EventHandler Handler; Protected void OnEvent(){ var handler = this.Handler; If(null!=handler){ handler(this, new EventArgs()); } }
Why does it assign a Handlerlocal variable before the call, except to directly call the event on Handler. Is there a difference between the two?
Handler
This is a typical way to avoid a race condition.
, , , if (Handler != null) Handler(this, EventArgs.Empty);. , , Handler null , , NullReferenceException.
if (Handler != null)
Handler(this, EventArgs.Empty);
null
NullReferenceException
, Handler , , , .
, :)
Source: https://habr.com/ru/post/1628865/More articles:Replace Single WhiteSpace without replacing multiple WhiteSpace - c #cannot connect: com.github.dockerjava.api.NotFoundException - dockerCppCheck warning: the expression depends on the evaluation order at x = xПостроение легенды с правильными метками python - pythonDjango Requested URL / not found on this server - pythonvar handler = MyEvent in VB.net - c #AllJoyn Thin Library Minimum Requirements - arduinoPython script not writing to file on execution at boot - pythonCalling a method after executing other methods - javascriptКак вызвать функцию, когда все вызовы ajax в функции завершены? - jqueryAll Articles