event.Invoke(args) vs event(args). Which is faster?

Writing someDelegate(...) is a compiler shorthand for someDelegate.Invoke(...).
They both compile to the same IL—a callvirt instruction to that delegate type's Invoke method.

The Invoke method is generated by the compiler for each concrete delegate type.

By contrast, the DynamicInvoke method, defined on the base Delegate type, uses reflection to call the delegate and is slow.


Since the introduction of null-conditionals in C# 6.0, Invoke can be used to simplify thread-safe null-checking of delegates. Where you would previously have to do something like

var handler = event;
if (handler != null)
    handler(args);

the combination of ?. and Invoke allows you to simply write

event?.Invoke(args)

When you call event(args), the C# compiler turns it into an IL call for event.Invoke(args). It's the same thing - like using string or System.String.

Tags:

C#

.Net