Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an actual difference in the 2 different ways of attaching event handlers in C#?

In C# is there any real difference (other than syntax) under the hood between:

myButton.Click += new EventHandler(myMemberMethod);

and

myButton.Click += myMemberMethod;

?

like image 569
DuckMaestro Avatar asked Oct 18 '08 01:10

DuckMaestro


People also ask

When would you use an event handler?

Use the EventHandler delegate for all events that do not include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event, and an object for event data).

What is an event handler give example?

In general, an event handler has the name of the event, preceded by "on." For example, the event handler for the Focus event is onFocus. Many objects also have methods that emulate events. For example, button has a click method that emulates the button being clicked.

What are event handlers in programming?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.

Which of the following is an event handler?

Answer: Function refers to an event handler.


1 Answers

The second method is a shortcut to the first one, it was introduced in C# 2.0

See also this thread.

like image 162
Alexander Kojevnikov Avatar answered Oct 06 '22 01:10

Alexander Kojevnikov