public event EventHandler Foo; public void OnFoo() { EventHandler handler = Foo; if (handler != null) { handler(this, EventArgs.Empty); } }
// , ! if (Foo != null) { // Foo null, // . Foo(this, EventArgs.Empty); }
public static void Raise(this EventHandler handler, object sender, EventArgs args) { if (handler != null) { handler(sender, args); } }
public void OnFoo() { Foo.Raise(this, EventArgs.Empty); }
public void OnFoo() { Foo?.Invoke(this, EventArgs.Empty); }
public void OnFoo() { Interlocked.CompareExchange(ref Foo, null, null)?.Invoke(this, EventArgs.Empty); }
public void OnFoo() { // .NET 4.5+, , ... Volatile.Read(ref Foo)?.Invoke(this, EventArgs.Empty); }
public event EventHandler Foo = delegate {} public void OnFoo() { // Foo will never be null Volatile.Read(ref Foo).Invoke(this, EventArgs.Empty); }
public event EventHandler Foo; public void OnFoo() { if (Foo != null) { Foo(this, EventArgs.Empty); } } .method public hidebysig instance void OnFoo() cil managed { // Code size 35 (0x23) .maxstack 3 .locals init ([0] bool V_0) IL_0000: nop IL_0001: ldarg.0 // this IL_0002: ldfld class [mscorlib]System.EventHandler A::Foo // Foo IL_0007: ldnull // null IL_0008: cgt.un // 2 (Foo null) - , 0 (false) IL_000a: stloc.0 // bool IL_000b: ldloc.0 // IL_000c: brfalse.s IL_0022 // false, IL_0022 (return) IL_000e: nop IL_000f: ldarg.0 // this IL_0010: ldfld class [mscorlib]System.EventHandler A::Foo // Foo - !!! null IL_0015: ldarg.0 // this IL_0016: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty // System.EventArgs::Empty IL_001b: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs) // Foo(this, EventArgs.Empty) IL_0020: nop IL_0021: nop IL_0022: ret } // end of method A::OnFoo
public event EventHandler Foo; public void OnFoo() { EventHandler handler = Foo; if (handler != null) { handler(this, EventArgs.Empty); } } .method public hidebysig instance void OnFoo() cil managed { // Code size 32 (0x20) .maxstack 3 .locals init ([0] class [mscorlib]System.EventHandler 'handler', [1] bool V_1) IL_0000: nop IL_0001: ldarg.0 // this IL_0002: ldfld class [mscorlib]System.EventHandler A::Foo // Foo, IL_0007: stloc.0 // Foo handler IL_0008: ldloc.0 // handler IL_0009: ldnull // null IL_000a: cgt.un // 2 (handler null) - , 0 (false) IL_000c: stloc.1 // bool IL_000d: ldloc.1 // IL_000e: brfalse.s IL_001f // false, IL_001f (return) IL_0010: nop IL_0011: ldloc.0 // handler IL_0012: ldarg.0 // this IL_0013: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty // System.EventArgs::Empty IL_0018: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs) // handler(this, EventArgs.Empty) IL_001d: nop IL_001e: nop IL_001f: ret } // end of method A::OnFoo
public event EventHandler Foo; public void OnFoo() { Foo?.Invoke(this, EventArgs.Empty); } .method public hidebysig instance void OnFoo() cil managed { // Code size 26 (0x1a) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 // this IL_0002: ldfld class [mscorlib]System.EventHandler A::Foo // Foo IL_0007: dup // Foo IL_0008: brtrue.s IL_000d // true null 0, IL_000d ( ) IL_000a: pop // - Foo ( , Foo == null) IL_000b: br.s IL_0019 // IL_000d: ldarg.0 // this ( , Foo != null) IL_000e: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty // EventArgs::Empty IL_0013: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs) // Invoke IL_0018: nop IL_0019: ret } // end of method A::OnFoo
public event EventHandler Foo; public void OnFoo() { Interlocked.CompareExchange(ref Foo, null, null)?.Invoke(this, EventArgs.Empty); } .method public hidebysig instance void OnFoo() cil managed { // Code size 33 (0x21) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 // this IL_0002: ldflda class [mscorlib]System.EventHandler A::Foo // Foo IL_0007: ldnull // null IL_0008: ldnull // null IL_0009: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [mscorlib]System.EventHandler>(!!0&, !!0, !!0) // Interlocked::CompareExchange IL_000e: dup // Foo - , Interlocked::CompareExchange IL_000f: brtrue.s IL_0014 // true null 0, IL_0014 ( ) IL_0011: pop // - Foo ( , Foo == null) IL_0012: br.s IL_0020 // IL_0014: ldarg.0 // this IL_0015: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty // EventArgs::Empty IL_001a: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs) // Invoke IL_001f: nop IL_0020: ret } // end of method A::OnFoo
public event EventHandler Foo; public void OnFoo() { Volatile.Read(ref Foo)?.Invoke(this, EventArgs.Empty); } .method public hidebysig instance void OnFoo() cil managed { // Code size 31 (0x1f) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 // this IL_0002: ldflda class [mscorlib]System.EventHandler A::Foo // Foo IL_0007: call !!0 [mscorlib]System.Threading.Volatile::Read<class [mscorlib]System.EventHandler>(!!0&) // Volatile::Read IL_000c: dup // Foo - , Volatile::Read IL_000d: brtrue.s IL_0012 // true null 0, IL_0012 ( ) IL_000f: pop // - Foo ( , Foo == null) IL_0010: br.s IL_001e // IL_0012: ldarg.0 // this IL_0013: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty // EventArgs::Empty IL_0018: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs) // Invoke IL_001d: nop IL_001e: ret } // end of method A::OnFoo
Source: https://habr.com/ru/post/272571/