C # event cleanup after execution?

Let's say I have the following code:

public event EventHandler DatabaseInitialized = delegate {};

//a intederminant amount of subscribers have subscribed to this event...


// sometime later fire the event, then create a new event handler...
DatabaseInitialized(null,EventArgs.Empty);

//THIS LINE IS IN QUESTION    
DatabaseInitialized = delegate {};

Will this eliminate subscribers by replacing it with a new blank default? And will the event notify all subscribers before they are cleared? I.E. Is there any chance of a race condition?

+5
source share
2 answers

Yes, it will clear. And since events occur synchronously in one thread, there should not be a race condition.

My advice: if in doubt, write a small test application and ... well, check it out.

UPDATE: I tested it before publishing. (In response to the cons.)

+7
source

unsubscribe event-=delegate, , . Microsoft , , , , , . unsubscribe .

0

All Articles