Events - naming convention and style

后端 未结 7 680
北海茫月
北海茫月 2020-12-02 10:40

I\'m learning about Events / Delegates in C#. Could I ask your opinion on the naming/coding style I\'ve chosen (taken from the Head First C# book)?

Am teaching a fr

7条回答
  •  既然无缘
    2020-12-02 10:57

    I would say the best guide to events in general, including naming conventions, is here.

    It is the convention I have adopted, briefly:

    • Events names are typically terminated with a verb ending with -ing or -ed (Closing/Closed, Loading/Loaded)
    • The class which declares the event should have a protected virtual On[EventName] which should be used by the rest of the class for raising the event. This method can be also used by subclasses to raise the event, and also overloaded to modify the event-raising logic.
    • There is often confusion about the use of 'Handler' - for coherence, all delegates should be postfixed with Handler, try to avoid calling the methods which implement the Handler 'handlers'
    • The default VS naming convention for the method which implements the handler is EventPublisherName_EventName.

提交回复
热议问题