Delegates and events in C #

Source: Internet
Author: User

Delegates and events in C #

Http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

    /// <summary>    ///bad design.///Additional language needs to be changed in the late 3 places: Language enumeration, Switch-case, Methods/// </summary>     Public classBaddesign { Public enumLanguage {中文版, Chinese//Add other language        }         Public voidGreetpeople (stringname, Language Lang) {            Switch(lang) { CaseLanguage.English:EnglishGreeting (name);  Break;  CaseLanguage.Chinese:ChineseGreeting (name);  Break; //Add other language            }        }         Public voidEnglishgreeting (stringname) {Console.WriteLine ("Good Morning,"+name); }         Public voidChinesegreeting (stringname) {Console.WriteLine ("Good Morning,"+name); }        //Add Language Method    } /// <summary>    ///a relatively good design///Additional language needs to be changed in late 1 places: Language method/// </summary>    classGooddesign_delegate { Public voidGreetpeople (stringname, Greetingdelegate makegreeting)        {makegreeting (name); }         Public voidEnglishgreeting (stringname) {Console.WriteLine ("Good Morning,"+name); }         Public voidChinesegreeting (stringname) {Console.WriteLine ("Good Morning,"+name); }        //Add Language Method    } Public Delegate voidGreetingdelegate (stringname); classProgram {Static voidMain (string[] args) {            NewBaddesign (). Greetpeople ("NLH", Delegate_Event.BadDesign.Language.Chinese); NewBaddesign (). Greetpeople ("NLH", Delegate_Event.BadDesign.Language.English); NewGooddesign_delegate (). Greetpeople ("NLH",Newgooddesign_delegate ().            chinesegreeting); NewGooddesign_delegate (). Greetpeople ("NLH",Newgooddesign_delegate ().            englishgreeting);        Console.read (); }    }

Delegates and events in C #

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.