I downloaded ReSharper and it is telling me to change this line:
dispMap.OnDraw += new EventHandler(dispMap_OnDraw);
To be this line:
dispMap.OnDraw += dispMap_OnDraw;
Because the first line is a "redundant delegate constructor call."
Is this true? In the automatically generated designer code for forms the syntax is based on the first piece of code and when typing in dispMap.OnDraw +=
and hitting TAB the IDE automatically generates new EventHandler(dispMap_OnDraw)
I'm just curious about this one. Does ReSharper have a point?
Yes, this is correct. I have done this in several cases.
The delegate constructor call should be implicit; the type can be inferred from
OnDraw
and validated against the method signature ofdispMap_OnDraw
.Also, a quote from this MSDN article appears relevant:
I believe the delegate instance is created either way, but since you don't have an object reference for the delegate when you implicitly instantiate, you can't remove it with the
-=
operator.It does have a point. The second line is shorthand for the first. Depending on your coding standards/conventions, you could use either one, but the first one does add a lot of noise.
it works fine, I have DevExpress and it tells me the same!
If you compare the IL generated in both cases, you'll see that they are the same. Here's both cases in C#, and the IL they result in.
Example C#:
Here's the IL for the constructor. Pay attention to lines
IL_000a
throughIL_0028
.Conclusion: I don't see any reason to change your code, they are equivalent.