Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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?

like image 653
bbosak Avatar asked May 08 '11 14:05

bbosak


People also ask

Which is the best place to subscribe event handler to an event?

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.

How events are invoked in C#?

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.

What is event args?

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.

What is delegate and event in C# with example?

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.


1 Answers

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.

like image 181
SLaks Avatar answered Sep 20 '22 06:09

SLaks