I'm reading Effective C# and I came across the following pattern to improve event-calling behavior and make it thread-safe:
public class EventSource {
private EventHandler<int> Updated;
private int counter;
public void RaiseUpdates(){
counter++;
var handler = Updated;
if (handler != null){
handler(this, counter);
}
The book claims that since there a "shallow copy" in the assignment to handler
, the call handler(this, counter)
will call all registered clients even if moments before one of them unsubscribed. But aren't delegates in C# reference types? Wouldn't this assignment just create a new reference for the underlying delegate object?