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

What is the commission of Dotnet?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the commission of Dotnet". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

Let's talk briefly about the origin of Delegate. In the early days of CUniverse +, there was a concept called function pointers. It's actually a memory pointer that points to a function. When calling a function, you only need to call the function pointer. As for the implementation of the function itself, it can be put somewhere else or later. When it comes to .net, there is no concept of pointers, but this approach is very practical, so the concept has been retained, forming the current delegate Delegate.

In addition, the delegate is extended in .net to design the same mechanism as the callback, allowing developers to define the signature and type of the callback.

When we declare a delegate, the compiler generates a class that derives from MulticastDelegate. MulticastDelegate also contains several methods, but because they are dynamically generated by the CLR runtime, they are not seen in the code IL and do not need to be concerned.

The biggest feature of delegates is that there is no need for strong coupling. So the caller does not know whether the method being called is static or instance, nor does it know the specific content of the call. A common example is the button Button class in UI programming. The button class itself does not know how its OnClick event is handled, nor does it need to know. So in practice, OnClick events are published using delegates. Developers implement the handling of OnClick events in the development process, and are used by UI subscriptions.

In this way, the delegate decouples the class.

Second, simple entrustment

A delegate has a very simple rule that the return type or parameter of the method to be referenced matches the delegate type declaration.

It sounds a little tongue-twisting. Let's take an example.

We have a way:

Void PrintInfo (string message)

According to the rule, the delegate method corresponding to this method can be written as follows:

Void Delegate_PrintInfo (string message)

In this way, according to the rules, when the delegate is used, it can be written as:

Delegate_PrintInfo = PrintInfo

Thus, when we call Delegate_PrintInfo ("Hello WangPlus"), what is actually executed is PrintInfo ("Hello WangPlus").

Next, let's look at the declaration of the delegate.

Public delegate int Delegate_Method (int x, int y)

Delegates can encapsulate any method. In the above example, we accept two parameters and return an int value.

In such a declaration, delegate is a keyword, indicating that we are declaring a delegate. The rest of it is no different from our normal way of coding.

Let me give you a few more examples:

Public delegate void Demo_Func1 (string para); public delegate ClassA Demo_Func2 (ClassB para); private delegate StructA Demo_Func3 (int para)

Except for delegate, the other content is no different from the normal method.

With the declaration, how to use it? Look at the example:

Class Program {public delegate int Delegate_Method (int x, int y); static void Main (string [] args) {Delegate_Method handler = SumMethod; int result = handler (3,4);} static int Sum (int x, int y) {return x + y;}}

This is a simple example.

We first define a delegate, accept two parameters, and return an int value. I want this delegate to call the following Sum method, so the Sum method is compatible with the signature (parameters and return value) of the delegate Delegate_Method. Here we should pay attention to understand that this concept of compatibility is not exactly the same, but compatibility.

Write a slightly more complicated example:

Public delegate void Delegate_Method (int x, int y); class ExampleClass {public void Sum (int x, int y) {Console.WriteLine (x + y);} public void Sub (int x, int y) {Console.WriteLine (x-y);}} class Program {static void Main (string [] args) {ExampleClass example = new ExampleClass (); Delegate_Method delegate_1 Delegate_Method delegate_2; delegate_1 = example.Sum; delegate_2 = example.Sub; delegate_1 (100,50); delegate_2 (100,50);}}

If the first example is clear, then this example is not difficult to understand.

III. Entrustment chain

The core of the delegate chain maintains a callable delegate list. When the list is called, all delegates in the list are called. At the same time, delegate chains can be combined with operators, combined with +, and deleted with -.

Look at the example:

Public delegate void Delegate_Method (int x, int y); class ExampleClass {public void Sum (int x, int y) {Console.WriteLine (x + y);} public void Sub (int x, int y) {Console.WriteLine (x-y);}} class Program {static void Main (string [] args) {ExampleClass example = new ExampleClass () Delegate_Method [] delegate_list = new Delegate_Method [] {example.Sum, example.Sub}; Delegate_Method delegate_chain = delegate_list [0] + delegate_list [1]; delegate_chain (100,50);}}

In this example, you define a delegate array and then combine these methods with the + operator.

Delegate_Method delegate_chain = delegate_list [0] + delegate_list [1]; Delegate_Method delegate_chain1 = delegate_chain-delegate_list [0]

The above two lines of code, CLR will be interpreted as (Sum + Sub)-Sum, and only execute the Sub method. This is an example of using the-operator to remove a delegate from a delegate chain.

You can also traverse the delegate chain:

Public delegate void Delegate_Method (int x, int y); class ExampleClass {public void Sum (int x, int y) {Console.WriteLine (x + y);} public void Sub (int x, int y) {Console.WriteLine (x-y);}} class Program {static void Main (string [] args) {ExampleClass example = new ExampleClass () Delegate_Method [] delegate_list = new Delegate_Method [] {example.Sum, example.Sub}; Delegate_Method delegate_chain = delegate_list [0] + delegate_list [1]; Delegate [] delegates = delegate_chain.GetInvocationList (); for (int I = 0; I < delegates.Length; iTunes +) {Delegate_Method _ delegate = (Delegate_Method) delegates [I] _ delegate (100,50);}

In this example, the GetInvocationList method is used to get all delegates in the delegate chain. This method helps us reference each delegate in the delegate chain, and we can also call the delegate in any order from the delegate chain.

IV. Multicast delegation

A delegate can call multiple methods when it is called, which is called multicast. A very useful property of delegate objects is that they can be assigned to a delegate instance for multicast using the + /-operator. A composite delegate invokes multiple delegates made up of it.

When multicasting delegates, only delegates of the same type can be combined. Operator can be used to add / remove delegate components from a composite delegate.

In addition, the multicast delegate return type is always void.

Class Program {public delegate void Delegate_Method (int x, int y); public static void Sum (int I, int j) {Console.WriteLine (I + j);} public static void Sub (int I, int j) {Console.WriteLine (I-j);} static void Main (string [] args) {Delegate_Method delegate1, delegate2, delegate3, delegate4; delegate1 = Sum Delegate2 = Sub; delegate3 = delegate1 + delegate2; delegate3 (100,50); delegate4 = delegate3-delegate2; delegate4 (100,50);}

In this code, delegate3 = delegate1 + delegate2; is equivalent to calling Sum one by one and Sub;delegate4 = delegate3-delegate2; is equivalent to calling (Sum + Sub)-Sub, which is actually called Sum.

This is the end of the content of "what is the commission of Dotnet". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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