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

前端 未结 6 1314
庸人自扰
庸人自扰 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 08:03

    Others have explained how to get round the issue, but not why it's coming up.

    When you declare a public field-like event, the compiler creates a public event, and a private field. Within the same class (or nested classes) you can get at the field directly, e.g. to invoke all the handlers. From other classes, you only see the event, which only allows subscription and unsubscription.

提交回复
热议问题