I wish to know whether a delegate has to be declared inside the class or outside the class. Because in both cases output is same.
Delegate and class
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
I wish to know whether a delegate has to be declared inside the class or outside the class. Because in both cases output is same.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ali AhmedRank #2,404 · 587 reputationPosted Nov 1, 2015, 7:00 AM EST
EventArgs, and then just useEventHandler- there's no need to create a separate delegate type. So instead ofAlarmEventHandler, you'd useEventHandler. TheEventArgs-derived class should generally be top-level (i.e. non-nested).I would declare them outside as they are ment to be used by other classes as well - the consumers of the event. I would keep the delegate in the same file as the class if the delegate is specific to this class.
It's less of the problem if you use generic version of
EventHandler, available in .NET 2.0 and newer, as you need to declare your customEventArgsonly.MahaRank #2,573 · 543 reputationPosted Nov 1, 2015, 9:27 AM EST
Article shows different ways of creating delegates. But it is not clarifying my doubt.
It says:
“It decouples the class that declares the delegate from the class that uses it”.
But whether it is decoupled or not, output is same. This is what my problem is.