C # delegate Case and understanding

Source: Internet
Author: User

The following uses the employee to play the game, the boss deduction salary case:

   Class Boss {private int money = 20;                    public void rsalary (int wage) {Console.WriteLine ("Boss found deduction of wages {0}¥, remaining {1}", Money,wage-money); }} class Employment {//In order to reduce the boss's work, while low coupling, use the delegate instead of the boss work public delegate void Pghandler (in        t pay);//define Parameters Delegate public event Pghandler pgevent;//Define event//boss bs = new Boss ();        public string name= "bin";        public string number= "S-0001";        public int wage=5000;            public void PlayGame () {Console.WriteLine ("Employee {0} play game, work number {1}", name, No.);            if (pgevent! = null) {//If there is an object registered pgevent (wage);//method that invokes all registered objects} Bs.        Rsalary ();//The most primitive, the employee plays the game to call the Boss class deduction salary method.            } public void Chat () {Console.WriteLine ("Employee {0}qq Chat, work number {1}", Name,number);            if (pgevent! = null) {pgevent (wage); }//bs.      Rsalary ();  }} class Program {static void Main (string[] args) {Employment em = new Employment ()            ;            Boss BS = new boss (); Em. Pgevent + = BS. rsalary;//Registration method em. PlayGame ();//play games, will automatically call the registration of the deduction method of wages em.            Chat ();        Console.ReadLine (); }    }}


C # delegate Case and understanding

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.