The previous article has demonstrated the use of delegation to implement a multi-language greeting program. This article will summarize the use of delegation in .net 1.x.
Since the delegate is a type (class), it must go through the process of first declaring a class, then new an object, and finally calling it.
The use of delegation in .net 1.x is as follows, the following code:
1 namespace DelegateDemo 2 { 3 //声明委托 4 public delegate void MyDel(string arg1, string arg2); 5 6 class Program 7 { 8 static void Main(string[] args) 9 {10 //.net 1.x中的委托11 Class1 c1 = new Class1();12 13 //创建委托对象14 MyDel myDel1 = new MyDel(c1.InstanceMethod);//实例方法15 MyDel myDel2 = new MyDel(Class1.StaticMethod);//静态方法16 17 //调用委托18 myDel1("a", "b");//或者myDel1.Invoke("a", "b");19 myDel2("a", "b");//或者myDel2.Invoke("a", "b");20 21 Console.ReadKey();22 }23 }24 25 public class Class126 {27 public void InstanceMethod(string arg1, string arg2)28 {29 Console.WriteLine(string.Format("arg1:{0},arg2:{1}", arg1, arg2));30 }31 32 public static void StaticMethod(string arg1, string arg2)33 {34 Console.WriteLine(string.Format("arg1:{0},arg2:{1}", arg1, arg2));35 }36 }37 }
It can be summarized from the above code:
1, the delegate can accept both instance methods and static methods, as long as the signature and return value type of the method match the delegate.
2. There are two ways to call a delegate. The first one is essentially the Invoke method of calling the delegate.
The above method is to use new DelegateType() to create a delegate. In fact, a simpler way can be used to create a delegate. The above code for creating a delegate can be simplified to:
1 //创建委托对象2 MyDel myDel1 = c1.InstanceMethod;//实例方法3 MyDel myDel2 = Class1.StaticMethod;//静态方法
In the above example, when creating a delegate, only A method is initialized. In fact, the delegate can add multiple methods. The adding method is implemented through '+=', and the removing method is implemented through '-='. The following code:
1 //创建委托对象2 MyDel myDel = c1.InstanceMethod;3 myDel += c1.InstanceMethod2;4 myDel += Class1.StaticMethod;5 myDel += Class1.StaticMethod2;
1 , consisting of multiple delegates is a chain delegation (or multicast delegation), and the System.MulticastDelegate class is designed for chain delegation.
2, += essentially calls the Delegate.Combine method.
Calling the delegate is as simple as calling the method. You only need to pass in the parameters required by the delegate. These parameters will be used to call each method in the delegate's method list, and in sequence Called sequentially, the following code:
1 //调用委托2 myDel("aaa","bbb");
The reason why chained delegates can be called sequentially is that System.MulticastDelegate internally maintains a pointer to the next delegate.
Output result:
Note:
1, if multiple identical methods are added to the delegate , then these methods will be called repeatedly.
2. If the delegate has a return value and the delegate's calling method list contains multiple methods, only the return value of the last method will be returned, and other return values will be ignored.
Finally, use XMind to summarize:
The above is the detailed content of Detailed explanation of delegate instances in .net 1.x. For more information, please follow other related articles on the PHP Chinese website!