Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Events with Delegate Handlers and those without? [duplicate]

Tags:

c#

events

What is the difference between this:

this.btnOk.Click += new System.EventHandler(this.btnOK_Click);

and this?

this.btnOk.Click += this.btnOK_Click;

They both work. The former is what Visual Studio defaults to when you use the snippets. But it seems like it only ads extra verbiage, or am I missing something?

like image 761
Kris Erickson Avatar asked Sep 23 '08 04:09

Kris Erickson


People also ask

What is the main difference between the event and delegate?

Delegate is a function pointer. It holds the reference of one or more methods at runtime. Delegate is independent and not dependent on events. An event is dependent on a delegate and cannot be created without delegates.

What is difference between delegate and method?

A delegate is a method with a parameter and a return type. A delegate is a type that safely encapsulates a method. Delegates are object-oriented, type safe, and secure.

How are delegates used in event handling?

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.

What is the use of delegates and events in C#?

A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered.


1 Answers

No difference. Omitting the delegate instantiation is just syntax candy; the C# compiler will generate the delegate instantiation for you under the hood.

like image 90
Judah Gabriel Himango Avatar answered Oct 31 '22 16:10

Judah Gabriel Himango