Possible Duplicate:
Should I Create a New Delegate Instance?
Hi, I’ve tried searching for the answer to this, but don’t really know what terms to search for, and none of the site-suggested questions are relevant. I’m sure this must have been answered before though.
Basically, can somebody tell me what’s the difference between these two lines in C#:
SomeEvent += SomeMethod
SomeEvent += new SomeDelegate(SomeMethod)
For example:
DataContextChanged += App_DataContextChanged;
DataContextChanged += new DependencyPropertyChangedEventHandler(App_DataContextChanged);
They both seem to do the same thing.
They are the same. The second variant is just a shorthand for the first called
Method group conversion.Simply put, the compiler infers what the type of the delegate is by using the delegate type of the event itself. This was introduced in C#2.0 if I’m not mistaken.