How does a C # eventhandler work internally?

I assume that C # evenhandler has a list of listeners, and it iterates through the list when sending a message. My question is how it works internally. Does he make a copy of the list before his cycle, and if so, what happens if someone unregisters after the list has been copied, but he has not yet received the message.

Will I still receive a message, even if it unregisters?

+5
source share
1 answer

The delegate is immutable, so when you call the delegate, the list of subscribers is known and fixed. Subscribing or unsubscribing replaces the delegate who backs up the event.

, , :

2, ( -ref ):

var handler = SomeEvent;
// <===== another thread could unsubscribe at this point
if(handler != null) handler(sender, args); // <== or part way through this invoke
// (and it either case, have the event trigger even though they think they have
// unsubscribed)

, , , , , , .

.

+6

All Articles