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.
Loading
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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MahaPosted Nov 1, 2015, 2:27 PM
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.
Ali AhmedPosted Nov 1, 2015, 12:00 PM
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.