This mathoperation class has three static methods, the parameters are double, and there is no return value, consider using action<> () This predefined delegate OH
classmathoperations { Public Static voidMultiplybytwo (Doublevalue) {Console.WriteLine ("0} = {1}", Value,value *2); } Public Static voidSquare (Doublevalue) {Console.WriteLine ("{0}* {1} = {2}", value, value, value*value); } Public Static voidIntroduction (Doublevalue) {Console.WriteLine ("I ' m a double. My value is {0}", value); } }
The book mentions the delegate array , and we define one. Put three methods inside.
action<double>[] Delegatelist = { mathoperations.multiplybytwo, Mathoperations.square, mathoperations.introduction };
I call this delegate this way.
foreach (var in delegatelist) { del (5); }
Output:
5* 5 = 25
5 = 10
I ' m a double. My value is 5
Try the multicast delegate + number again.
action<Double> operations = mathoperations.multiplybytwo; + = Mathoperations.square ; + = mathoperations.introduction; Operations (5);
The result is the same.
But the book says it's a good idea.
Public Static void Processanddisplaynumber (action<doubledouble value) { operations (value); }
Give a chestnut, hit, relatives, curse, pinch people, these 4 actions all need a person as your object of action. I am an action entrusted <in person, you can let me hit, Curse, Curse * *, all of a sudden to perform a number of actions, including repetitive movements. That's what I understand, at the moment.
"C # advanced" Multicast delegates and delegate arrays like it's one thing ~