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