[.net] Event/delegate question...

Started by
4 comments, last by Telastyn 17 years, 11 months ago
Is there a difference between: foo.Event += new EventHandler( bar ); and foo.Event += bar; ?
Advertisement
No.
Thanks. I knew the behaved the same way, but I wasn't sure if one was preferred to the other or something.
On my dev team we tend to prefer the second form. However, one advantage of the first form is that VS will autogenerate the method signature of the handler for you.
Note that the second is only legal in C#2. C#1 didn't do implicit delegate type discovery. So if you're planning on downlevel build support, avoid it.
[sub]My spoon is too big.[/sub]
Also, if you're in the unlikely scenario of passing something to just a System.Delegate (arbitrary delegate) this:

System.Delegate Stuff+=delegate(string s){ /*...*/ };


doesn't work. Object return isn't assumed, and it can't otherwise determine.
Explicitly setting the type is required.

This topic is closed to new replies.

Advertisement