Possible Duplicate:
C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'
In C# .NET 3.5, the compiler doesn’t care if I do this (assume ButtonClickHandler is a function):
button.OnButtonClicked += ButtonClickHandler;
or:
button.OnButtonClicked += new ButtonClickHandlerDelegate( ButtonClickHandler );
Are these functionally the same? I read the stackoverflow question below, but I’m not sure if it applies to this scenario as well:
The difference between implicit and explicit delegate creation (with and without generics)
Yes, the first is simply syntactic sugar for the latter. The compiler simply infers the type of the delegate and constructs it for you. The exact same IL will be emitted by the compiler.
The first, shorter and cleaner syntax (delegate inference – which I recommend you use for readability), was added in C#2 – that is why some designers (also Microsoft’s) tend to use the long and more verbose syntax of
newing the delegate.Actually, I think this is a duplicate of this prior question.