Which is faster; using event.Invoke(args)
, or just calling event(args)
. What's the difference? Is one faster or slower than the other; or is it just a matter of preference?
To subscribe to events by using the Visual Studio IDEOn top of the Properties window, click the Events icon. Double-click the event that you want to create, for example the Load event. Visual C# creates an empty event handler method and adds it to your code. Alternatively you can add the code manually in Code view.
The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers. In a typical C# Windows Forms or Web application, you subscribe to events raised by controls such as buttons and list boxes.
The EventArgs class is the base type for all event data classes. EventArgs is also the class you use when an event doesn't have any data associated with it.
A delegate is a way of telling C# which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer that is a delegate. Delegates are good, as you can notify several methods that an event has occurred, if you wish so.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With