In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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: 263
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.