"Go" writing high-quality Code 157 recommendations for improving C # Programs--Recommendation 139: event handler naming combined

Source: Internet
Author: User

Recommendation 139: Event handler naming takes a combined approach

The so-called event handler is the method that is actually commissioned. See the following code:

         PublicMainWindow () {InitializeComponent (); Button Button=NewButton (); button. Click+=Button_Click; button. SizeChanged+=button_sizechanged; button. MouseDown+=Button_mousedown; }        voidButton_Click (Objectsender, EventArgs e) {            Throw Newnotimplementedexception (); }        voidButton_sizechanged (Objectsender, EventArgs e) {            Throw Newnotimplementedexception (); }        voidButton_mousedown (Objectsender, MouseEventArgs e) {            Throw Newnotimplementedexception (); }

In this code, the methods Button_Click, button_sizechanged, Button_mousedown are called event handlers. vs defaults to the naming rules for the event handlers we generate:

Event variable owning object + underscore + Event variable name

This named method is used to register the method (the "+ =" operator) to add the event handler. If we are writing a processor for a callback in a delegate or delegate, you should use the following naming convention:

Delegate variable owning object +on+ delegate variable name

As follows:

         Public BOOL Nameonvalidatevalue (object  value)        {                    }        publicvoid  Nameonpropertychanged (DependencyObject D, DependencyPropertyChangedEventArgs e)        {                    }

Name of the failure:

         Public void namechanged (DependencyObject D, DependencyPropertyChangedEventArgs e)        {                    }

This name should look more like a delegate or an event variable.

Of course, if the processor is inside the class, you can omit the previous type and prefix it directly with on.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

Go to write high-quality Code 157 recommendations for improving C # Programs--Recommendation 139: Event handler naming takes combination

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.