How virtual events work in C#?

前端 未结 3 775
眼角桃花
眼角桃花 2020-12-06 06:22

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

3条回答
  •  [愿得一人]
    2020-12-06 07:23

    You hooked up two event handlers to the same event. Since A and B are pointing to the same object, when you call b.RaiseB() both event handlers get fired. So first you're calling RaiseA which is a basecalss method. That prints Raise A. It then doesn't actually fire off the event because it's null. Then, you're raising B but TWO handlers are hooked up to it, therefore it first prints Raise B, and when the event fires, both handlers get called.

提交回复
热议问题