(From what I read in Essential C# 4.0)
Basically, from this C# code:
public class CustomEventArgs: EventArgs {…}
public delegate void CustomEventHandler(object sender, CustomEventArgs a);
public event CustomEventHandler RaiseCustomEvent;
the compiler will generate CIL code (loosely) equivalent to the following C# code:
public delegate void CustomEventHandler(object sender, CustomEventArgs a);
private CustomEventHandler customEventHandler; // <-- generated by the compiler
public void add_CustomEventHandler(CustomEventHandler handler) {
System.Delegate.Combine(customEventHandler, handler);
}
public void remove_CustomEventHandler(CustomEventHandler handler) {
System.Delegate.Remove(customEventHandler, handler);
}
public event CustomEventHandler customEventHandler {
add { add_customEventHandler(value) }
remove { remove_customEventHandler(value) }
}
When you copy the event, you actually copy the private CustomEventHandler customEventHandler
. Since delegate is immutable, the copy won't be affected when the original customEventHandler
is modified. You can try this code to see what I mean:
string s1 = "old";
string s2 = s1;
s1 = "new"; // s2 is still "old"
Another important characteristic to note about the generated CIL
code is that the CIL equivalent of the event
keyword remains in the CIL.
In other words, an event is something that the CIL code recognizes
explicitly; it is not just a C# construct. By keeping an equivalent event
keyword in the CIL code, all languages and editors are able to provide
special functionality because they can recognize the event as a special
class member.
I guess you were confused mainly because you thought event is a sugar-syntax for a class, right?