I may be misunderstanding something fundamental here as I’m new to these concepts so please bear with me.
I’m currently removing methods from an event like so:
scheduleView.TouchDown -= scheduleView_TouchDown;
And then on other occasions – adding the methods:
scheduleView.TouchDown += scheduleView_TouchDown;
It all works fine so far, and I can understand it’s possible to add several methods, like so:
scheduleView.TouchDown += scheduleView_TouchDown;
scheduleView.TouchDown += scheduleView_AnotherTouchDownEventHandler;
But how would I then later check what methods were wired up to this event?
Interestingly, you can’t (at least, from the outside). An
eventis only obliged to offer 2 accessors –addandremove. There are other accessor methods defined in the CLI spec, but they aren’t used in C# or anywhere else AFAIK. The key point: we can’t ask aneventwhat is subscribed (and indeed, we shouldn’t need to know). All you can do is:addorremove.If you are worried about double-subscribing, then note that if you try to unsubscribe and you haven’t actually subscribed, then under every sane implementation this is simply a no-op; which means you can do:
From the perspective of the code raising the
event, you rarely need to know who – simply:There is also a way to break the delegate list into individual subscribers, but it is very rarely needed:
The main uses for this are: