Events - naming convention and style

后端 未结 7 651
北海茫月
北海茫月 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 11:20

    Microsoft has actually written extensive set of naming guidelines and put it in the MSDN library. You can find the articles here: Naming Guidelines

    Aside from the general capitalization guidelines, here is what it has for 'Events' on the page Names of Type Members:

    ✔️ DO name events with a verb or a verb phrase.

    Examples include Clicked, Painting, DroppedDown, and so on.

    ✔️ DO give events names with a concept of before and after, using the present and past tenses.

    For example, a close event that is raised before a window is closed would be called Closing, and one that is raised after the window is closed would be called Closed.

    ❌ DO NOT use "Before" or "After" prefixes or postfixes to indicate pre- and post-events. Use present and past tenses as just described.

    ✔️ DO name event handlers (delegates used as types of events) with the "EventHandler" suffix, as shown in the following example:

    public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);
    

    ✔️ DO use two parameters named sender and e in event handlers.

    The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type.

    ✔️ DO name event argument classes with the "EventArgs" suffix.

    0 讨论(0)
提交回复
热议问题