Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Create a New Delegate Instance?

Tags:

c#

delegates

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?

like image 435
Scott Avatar asked Aug 27 '10 15:08

Scott


People also ask

What is advantage of using delegates?

Benefits of DelegatingGives you the time and ability to focus on higher-level tasks. Gives others the ability to learn and develop new skills. Develops trust between workers and improves communication. Improves efficiency, productivity, and time management.

Are delegates type safe?

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegates in C# to implement events and call-back methods. A multicast delegate is one that can point to one or more methods that have identical signatures.

How do we create an instance of a delegate?

Use the new keyword to instantiate a delegate. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.


1 Answers

The 2nd syntax is a shortcut for the 1st one introduced in C# 2.0.

http://www.developer.com/net/csharp/article.php/3103031/Working-with-Delegates-Made-Easier-with-C-20.htm

like image 72
Jerome Avatar answered Oct 12 '22 23:10

Jerome