Why events can't be used in the same way in derived classes as in the base class in C#?

前端 未结 6 1317
庸人自扰
庸人自扰 2020-11-29 07:27

In following code, I want to extend the behaviour of a class by deriving/subclassing it, and make use of an event of the base class:

public class A
{
    pub         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 07:44

    The reason the original code doesn't work is because you need to have access to the event's delegate in order to raise it, and C# keeps this delegate private.

    Events in C# are represented publicly by a pair of methods, add_SomeEvent and remove_SomeEvent, which is why you can subscribe to an event from outside the class, but not raise it.

提交回复
热议问题