Should I Create a New Delegate Instance?

2019-01-26 09:28发布

What are the implications of doing this...

this.myButton.Click += new EventHandler(this.myButton_Clicked);

...versus this?

this.myButton.Click += this.myButton_Clicked;

I suspect that the compiler is creating a new instance for me in the second example. I'm sure this is a bit of a newbie question, but Google didn't turn up anything. Can anyone give me some insight?

标签: c# delegates
2条回答
Ridiculous、
2楼-- · 2019-01-26 10:01
欢心
3楼-- · 2019-01-26 10:05

Yes, the second version makes the compiler create an implicit delegate, much like you can specify this.MyMethod instead of new Action(this.MyMethod) or new Action(() => this.MyMethod()).

查看更多
登录 后发表回答