Unsubscribe from a .NET event from a subscriber

I was asked a very interesting question about the events. I think the answer is no, but I'm curious if there is a way that I don't know about.

in the following code example, can I remove a subscription from reference class B or its method X ??

Class1 A = new Class1(); Class2 B = new Class2(); A.DoneIt += BX; 

means the execution of A.DoneIt -= BX; without any access to class A (or by reflection in class A).

+4
source share
1 answer

No, you can unsubscribe if you can access the event.

Delegates point one way, i.e. on the method. The method does not have an event pointer. Thus, the only access to the event for unsubscribing is through an instance of the type that it has defined.

Further reading: Events and delegates

+6
source

All Articles