c# - How does an EventHandler know to allow = operator only in defining class? -
I started with a question, and in answering the question, got the answer, but instead of deleting, I thought it May be helpful from A), and b) Help others.
If I have an event, and add listeners to many places in the application, then what is the best way to remove all the listeners at once? For example, I ...
myPage.OnPageOpened + = OpenPage; and later in my code ...
myPage.OnPageOpened - = OpenPage; But what if I have an unknown third party member and I can press a magic reset button that clears everything and starts with scratch?
You can use + = - - Operator anywhere against = EventHandler, because operator overload is public = overloaded operator is private, it can only be said in defined category.
So in the defined category, I can use it to clean my EventHandler.
OnPageOpened = null; And to expose that functionality, I could ...
Public Zero ClearPageOpenedEvents () {OnPageOpened = null; } Is that correct?
Yes, you are right. The reason for this is that the compiler creates a personal representative object under the cover, like this:
Private EventHeader page opens; Public event handler page request {{pageOpened + value = add; } Remove {pageOpened - = value; }} Inside your class, you have a reference of a private representative example, so you can do the assignment. You definitely want to expose a method to clear the goal, if you need it; You do not want to expose your representative .
Comments
Post a Comment