Below is the program I used for the test. It prints (as expected):
Raise A
Event from A
Raise B
Event from B
Now, if we change first two li
We have a single instance (of B) which has the following fields:
The call to a.RaiseA()
just prints "Raise A" - but nothing more, because the private field in A is null.
The call to b.RaiseB()
prints the remaining three lines, because the event has been subscribed to twice (once to print "Event from A" and once to print "Event from B").
Does that help?
EDIT: To make it clearer - think of the virtual event as a pair of virtual methods. It's very much like this:
public class A
{
private EventHandler handlerA;
public virtual void AddEventHandler(EventHandler handler)
{
handlerA += handler;
}
public virtual void RemoveEventHandler(EventHandler handler)
{
handlerA -= handler;
}
// RaiseA stuff
}
public class B : A
{
private EventHandler handlerB;
public override void AddEventHandler(EventHandler handler)
{
handlerB += handler;
}
public override void RemoveEventHandler(EventHandler handler)
{
handlerB -= handler;
}
// RaiseB stuff
}
Now is it clearer? It's not quite like that because as far as I'm aware you can't override just "part" of an event (i.e. one of the methods) but it gives the right general impression.