6

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?

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175

4 Answers4

10

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

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
4

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
  • 19,053
  • 13
  • 51
  • 67
1

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.

Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
1

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

aku
  • 122,288
  • 32
  • 173
  • 203