Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement delegation in C #

Shulou Source: shulou.com Published: 2022-06-03 02:21:37 10月13日 Update

This article mainly introduces how to achieve the commission of C#, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Let's start with a very simple example:

Namespace LinQ {/ / declares a delegate that can point to any method public delegate int MathOP (int I, int j) that passes in two integers and returns one integer; / / this class contains the method Add class DelegateMath {public static int Add (int I, int j) {return I + j that MathOP will point to. }} class Program {static void Main (string [] args) {/ / create a delegate MathOP o = new MathOP (DelegateMath.Add) pointing to DelegateMath.Add (); / / use the delegate to indirectly call the Add () method Console.WriteLine ("10mem20 = {0}", o (10mem20) / / or you can do that. If you don't know o.Invoke, please read on. If you know, then this article can ignore Console.WriteLine ("10: 20 = {0}", o.Invoke (10, 20)); Console.ReadLine ();}

The result of the operation is:

From this simple example, you can see that the use of a delegate is very simple. If you want to insert a target object method into a specified delegate, you can simply pass the method name to the delegate's constructor. Of course, this method is exactly the same as the return value when defining the delegate, the parameter type and the number. This is the member that we can use a syntax similar to direct function calls to point to: DelegateMath.Add (int iPaper int j)

Let's move on to an in-depth discussion of the commission. Things, we can't just look at the surface, especially the sex in society. Needless to say, "in-depth", first look at a picture:

This is the result of decompilation of a decompiler, mainly looking at the LinQ.MathOP selected in the middle, which is our defined delegate, which is a class, but we didn't add so many methods to this class that we don't know where they came from. In fact, when the C # compiler deals with delegate types, it automatically generates a sealed class derived from System.MulticastDelegate. This class, along with its base class, System.Delegate, provides the necessary infrastructure for delegates. You can see that the generated MathOP class defines three public methods. Invoke should be the core method because it is used to call each method maintained by the delegate type in a synchronous manner, which means that the caller must wait for the call to complete before continuing. The BeginInvoke () and EndInvoke () methods can asynchronously call the current method on the second thread of execution, which are mainly used in multithreading. Now you just need to know that there is such a thing, which will be explained continuously in future blog posts. The pseudocode for LinQ.MathOP should look like this:

Sealed class MathOP:System.MulticastDelegate {public MathOP (object target,uint functionAddress); public int Invoke (int iminute int j); public IAsyncResult BeginInvoke (int iminute int jpg AsyncCallback cb,object state); public int EndInvoke (IAsyncResult result);}

Finally, delegates can also point to any number of out or ref parameters (and arrays marked with the params keyword). If the out or ref parameters are included, the signatures of the Invoke () and BeginInvoke () methods are the same, but the EndInvoke () method changes slightly, including all out or ref parameters defined by the delegate type, which, of course, doesn't require our concern.

Thank you for reading this article carefully. I hope the article "how to entrust C#" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Delegate method point article parameter type Category this is compile east-west two example function integer thread result synchronization generation clear necessary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB MySQL macOS Redmi