Is it poor form for a C# class to subscribe to its own published events?

后端 未结 6 737
难免孤独
难免孤独 2020-12-16 17:06

I\'m probably just being neurotic, but I regularly find myself in situations in which I have class that publishes an event, and I find it convenient to subscribe to this eve

6条回答
  •  春和景丽
    2020-12-16 17:46

    If your button class should be the first which receives the click event, you should write your code in the event method, eg.:

    protected virtual void OnClick(EventArgs e)
    {
        //insert your code here
    
        if(this.Click != null)
        {
            this.Click(this, e);
        }
    }
    

    but if it's not necessary that your class is the first reciever, you can subscribe to the event normally.

提交回复
热议问题