noctonura
noctonura

Reputation: 13121

Should I new-up a new delegate or just add the method to an event?

I don't understand what the difference between these 2 variations are. What is the pros/cons of each approach?

1.  a.MyEvent += new MyClass.MyEventDelegate(FireEvent);

2.  a.MyEvent += FireEvent;

Upvotes: 5

Views: 101

Answers (2)

Quintin Robinson
Quintin Robinson

Reputation: 82335

Really it's syntactical sugar, the compiler will interpret the code and make the Delegates for you.

Upvotes: 2

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421988

The first one works in all versions of C# while the second only works on 2.0 and above. If you need your to compile you code with C# 1.0 compiler, go with the first one; otherwise, I'd use the more concise version. The generated code should be identical in both cases.

Upvotes: 6

Related Questions