Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of delegation in .net 1.x

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis entrusted in .net 1.x, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it together!

Delegate in .net 1.x

The use of delegates in .net 1.x looks like this, as follows:

1 namespace DelegateDemo 2 {3 / declare delegate 4 public delegate void MyDel (string arg1, string arg2); 56 class Program 7 {8 static void Main (string [] args) 9 {10 / / .net 1.x delegate 11 Class1 C1 = new Class1 (); 12 13 / / create delegate object 14 MyDel myDel1 = new MyDel (c1.InstanceMethod) / / instance method 15 MyDel myDel2 = new MyDel (Class1.StaticMethod); / / static method 16 17 / call delegate 18 myDel1 ("a", "b"); / / or myDel1.Invoke ("a", "b"); 19 myDel2 ("a", "b"); / / or 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}

From the above code, you can sum up:

1. A delegate can accept both instance and static methods, as long as the signature and return value type of the method matches the delegate.

2. There are two ways to invoke a delegate, and the first is essentially a call to the delegate's Invoke method.

Assignment delegation

The delegate is created using new DelegateType () above, but you can actually create the delegate in a simpler way. The above code to create a delegate can be simplified to:

1 / / create delegate object 2 MyDel myDel1 = c1.InstanceMethod alternate / instance method 3 MyDel myDel2 = Class1.StaticMethod;// static methods add and remove methods for delegate

In the above example, only one method is initialized when the delegate is created. In fact, a delegate can add multiple methods, adding a method through'+ = 'and removing a method through'-=', as shown in the following code:

1 / / create delegate object 2 MyDel myDel = c1.InstanceMethodist3 myDel + = c1.InstanceMethod2t4 myDel + = Class1.StaticMethod;5 myDel + = Class1.StaticMethod2

1. A chained delegate (or multicast delegate) is composed of multiple delegates, and the System.MulticastDelegate class is designed for chained delegates.

Essentially, the Delegate.Combine method is called.

Invoke delegate

Calling a delegate is as simple as calling a method. You only need to pass in the parameters required by the delegate, which will be used to call each method in the delegate's method list, and are called sequentially, as shown in the following code:

1 / / call delegate 2 myDel ("aaa", "bbb")

Chained delegates can be called in turn because System.MulticastDelegate maintains a pointer to the next delegate.

Output result:

Note:

1. If multiple identical methods are added to the delegate, these methods will be called repeatedly.

2. If the delegate has a return value and the delegate's list of calling methods contains multiple methods, only the return value of the last method will be returned, and all other return values will be ignored.

Finally, use XMind to sum up:

The above is all the contents of the article "sample Analysis entrusted in .net 1.x". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report