About entrust oneself in the work seldom encounter, gradually feel is not very important, but in the written test is often tested, so turned to write the data to see a bit, the delegate name is called to give things to others to do, I will be a story of the way to explain what the delegation is.
I've got two people in here.
Pmwang
//1.0 Lao Wang intends to add a computer Private int_num =1; Public voidBuycomputer () {//2.0 but no time! Tell Xiao Li to buy it? Console.WriteLine ("Xiao Li goes to buy a computer"); //5.0 Ask Xiao Li to buybuydelegate (_num); } //3.0 Lao Wang is going to ask Xiao Li to buy this place and define a delegate. Public Delegate voidLetlibuycomputer (intnum); //4.0 declaring this delegate PublicLetlibuycomputer buydelegate;
SELi
//Xiao Li has a way to buy a computer. Public voidBuycomputer (intnum) {Console.WriteLine ("Okay, good! "); Console.WriteLine ("out"); Console.WriteLine ("by car"); Console.WriteLine ("Buy to a computer"); Console.WriteLine ("Back to company"); Console.WriteLine ("Li: Manager Wang I bought a computer for one day"); }
Main ()
Static voidMain (string[] args) { //1.0 Old King's here.Pmwang Wang =NewPmwang (); //2.0 Xiao Li is here.SELi Li =NewSELi (); //4.0 The old King's commission first look at 3.0 good understanding of this delegation process note: cannot be changed in programmingWang.buydelegate =NewPmwang.letlibuycomputer (li. Buycomputer); //3.0 Lao Wang buys a computerWang. Buycomputer (); Console.read (); }
When performing Wang. Buycomputer () Call old Wang buy computer, Lao Wang do not want to go to buy a computer a series of procedures entrusted to Xiao Li, small Lee Buycomputer () is to perform the action of buying a computer. This is the commissioning of the purchase of computer methods, the action of the performer is Xiao Li's buycomputer ().
C # Delegate