tags:

views:

1095

answers:

4

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?

A: 

I believe that C# since 3.0 has implicitly added the delegate handler. However, it can help to be more explicit, especially when there are multiple possible delegate types.

Cody Brocious
+1  A: 

"+= Delegate_Name" is a syntax sugar. Compiler will create new wrapper for you.

aku
+8  A: 

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

Judah Himango
+3  A: 

In C# 3.0 and later this is no difference. Before C# 3.0 EventHandlers were required due to compiler limitations, but with the advent of C# 3.0, the second form is preferred unless you want to be very explicit.

Orion Adrian