157 recommendations for writing high-quality code to improve C # programs--Recommendation 40: Use the event keyword to enforce protection for delegates

Source: Internet
Author: User

Recommendation 40: Use the event keyword to apply protection to delegates

In the proposal we implemented a file transfer class with the notification function, as follows:

    classFileuploader { Public Delegate voidFileuploadedhandler (intprogress);  PublicFileuploadedhandler fileuploaded;  Public voidUpload () {intFileprogress = -;  while(Fileprogress >0)            {                //transfer code, omitfileprogress--; if(fileuploaded! =NULL) {fileuploaded (fileprogress); }            }        }    }

Call like this:

        Static voidMain (string[] args) {Fileuploader F1=NewFileuploader (); F1. fileuploaded=Progress; F1. fileuploaded=Progressanother; F1.                        Upload ();        Console.read (); }        Static voidProgress (intprogress)        {Console.WriteLine (progress); }        Static voidProgressanother (intprogress) {Console.WriteLine ("Another method: {0}", progress); }

The above caller code itself is in conjunction with the Fileuploader class, which has at least two questions:

1) If another thread is in main, the worker thread can set the fileprogress delegate chain to null:

null;

2) fileuploaded can be called externally, such as:

F1. Fileuploaded (ten);

This should not be allowed, because when the caller is notified, it should be the Fileuploader class's own responsibility, rather than the caller itself to decide. The event keyword is raised in this case, and it is protected by the delegate.

Will

 Public Fileuploadedhandler fileuploaded;

Switch

 Public Event Fileuploadedhandler fileuploaded;

In this way, several of the above mentioned situations are blocked:

Null== progressanother;

The above code will compile without passing:

Event "MyTest.FileUploader.FileUploaded" can only appear to the left of + = or-= (except when used from type "Mytest.fileuploader")

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

157 recommendations for writing high-quality code to improve C # programs--Recommendation 40: Use the event keyword to enforce protection for delegates

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.