What is the difference between Events with Delegat

2020-04-05 08:33发布

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?

标签: c# events
4条回答
甜甜的少女心
2楼-- · 2020-04-05 09:00

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.

查看更多
劳资没心,怎么记你
3楼-- · 2020-04-05 09:02

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

查看更多
淡お忘
4楼-- · 2020-04-05 09:02

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

查看更多
叼着烟拽天下
5楼-- · 2020-04-05 09:03

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.

查看更多
登录 后发表回答