"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 41: Implementing a standard event model

Source: Internet
Author: User

Recommendation 41: Implementing a standard event model

In the previous recommendation, we implemented a file transfer class Fileuploader with event notification. Although the requirements have been met, but do not conform to C # coding specifications, view EventHandler's prototype declaration:

 Public Delegate void EventHandler (object sender, EventArgs e);

We should know a few of the specifications Microsoft defines for the event model:

    • The name of the delegate type has EventHandler ended;
    • The return value of the delegate prototype is void;
    • The delegate prototype has two parameters: Sender represents the event trigger, and E represents the event argument;
    • The name of the event argument ends with EventArgs.

To modify Fileuploader to conform to the C # Programming specification, you first need to provide a Fileuploadedeventargs class to hold the progress information:

    classFileuploadedeventargs:eventargs { Public intfileprogress {Get;Set; } }    classFileuploader { Public EventEventhandler<fileuploadedeventargs>fileuploaded;  Public voidUpload () {Fileuploadedeventargs e=NewFileuploadedeventargs () {fileprogress = - };  while(E.fileprogress >0)            {
          //Transfer code, omit e.fileprogress--; if(fileuploaded! =NULL) {fileuploaded ( This, E); } } } }

Calling code:

        Static void Main (string[] args)        {            Fileuploader f1=new  fileuploader ();             + = f1_fileuploaded;
F1. Upload (); } Static void f1_fileuploaded (object sender, Fileuploadedeventargs e) { Console.WriteLine (e.fileprogress); }

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

"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 41: Implementing a standard event model

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.