class MeiTuan { // declare a delegate public the delegate String OrderDelegate ( int Money, String Order); public String OrderEnter ( int Money, String Order, OrderDelegate oederDelegate) { // business logic Store = Store new new Store (); store.Receipt (Money , Order); store.Finish (); // can be packaged more business logic return oederDelegate (Money, Order); } } class Store { public void the Receipt ( int Money, String Order) { Console.WriteLine ( " ごas rice ga ~ te su i ma ...... " ); } public void Finish () { Console.WriteLine ( " ごrice Coming ...... .. " ); } } class PaoTui { // callback function [function as an argument to another function} public String BuyFood ( int mongey, String Order) { return " ごっbuy rice ta ga ....."; } } class Program { static void Main(string[] args) { MeiTuan member1 = new MeiTuan(); PaoTui brother = new PaoTui(); string mess = member1.OrderEnter(66, "rice", brother.BuyFood); Console.WriteLine(mess); Console.ReadKey(); } }
Original link: https: //blog.csdn.net/wxfx888/article/details/78079970
event:
Definition: when a particular action occurs, to execute a section of code. Nature of the event is to delegate. Such as a button click event, keyboard depression event. . .
Commission:
- Definition: A delegate is a class. It is a definition of the type of method signature. When the delegate instance, you may be instances associated with any method with a compatible signature. You can call the method through a delegate instance. Signature is a method parameters, return type.
For example: life, if we need if a lawsuit in court by lawyers for our defense, but lawyer Barbara is executed heard the parties, this time the lawyer was a delegate object, the object is to help the parties a lawyer to defend himself.
However, the concept in C # objects entrusted to it like a lawyer.
2. How to use:
I. Definitions: delegate void Mydelegate (type1 para1, type2 para2);
Second, the statement: Mydelegate d;
Third, the instantiation: d = new Mydelegate (obj.InstanceMethod); (pass a constructor method to a delegate), steps in front of a lawyer configured like object like a party methods InstanceMethod
Fourth, as a method to pass parameters: MyMethod (d); (delegate implement the method of [] obj.InstanceMethod passed as a parameter to another method MyMethod [], the object delegate is a method of packaging)
Fifth, use the delegate methods. MyMethod method is like a judge, MyMethod method first call delegate, delegate invocation method InstanceMethod, this process just as a judge for questioning to a lawyer, then certainly understand the circumstances of the case to the parties before a lawyer. C # delegate is like a lawyer, Barbara tells the merits of the parties (Barbara is called an instance method InstanceMethod)
Sixth, the commission can achieve superimposition method, the formation of the list of delegates. For example: d + = Method2 ();
3. Role:
First, the introduction of commission, the programmer can process references encapsulated delegate object (the calling procedure into an object invocation, fully reflects commissioned enhanced object-oriented programming ideas.), Then passed delegate object in need code that references the method, so that in the process of compiling, we do not know what to call a method, this way, C #
After the introduction of delegation mechanism, so that the method declaration and implement methods of separation, fully embodies the object-oriented programming ideas.
Second, I am a special class , I define the type of method (as defined in the same numeric type int, when the delegate object is instantiated with a method, the delegate represents a method, the method of this type is a delegate type ), method I may be used as the parameter to be passed to another process, the program makes it easier to expand. Namely decoupling.
4 illustrates a delegate
As the code, customers want to call on the US group to run errands service, just give him the money and told him what I want to buy, who do not need to know how to help me to buy the stuff.
Callback
The callback function is passed to a method of another way to do it. Callback function is a functional fragment, a function according to the user callback function calling conventions implemented. Delegate understood conjunction with the above codes.