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

How to write different versions of .NET delegation

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

Share

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

This article mainly explains "how to write different versions of .NET delegation". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write different versions of .NET delegation.

The Writing of delegation in .NET 1.x

Delegate, if we do not look into the details, on the surface, we can understand it as a secure "function pointer". Of course, this function pointer is also an object, has its own members, and encapsulates the context of the callee, and so on. As for the definition and use of delegates, it goes like this:

Public delegate int SomeDelegate (string arg1, bool arg2); public static int SomeMethod (string arg1, bool arg2) {return 0;} public class SomeClass {public int SomeMethod (string A1, bool a2) {return 0;} public event SomeDelegate SomeEvent;} static void Main (string [] args) {SomeClass someClass = new SomeClass (); SomeDelegate someDelegate = new SomeDelegate (someClass.SomeMethod); someClass.SomeEvent + = new SomeDelegate (SomeMethod);}

It can be seen that new DelegateType (...) needs to be used in .NET 1.x. To create a delegate object. However, as a method inside a delegate object, it can be either an instance method or a static method. In addition, the method only needs to match the signature and return value of the delegate type, and the name of the method parameter does not become a constraint.

Well, it's that simple.

The Writing of delegation in .NET 2.0

The .NET delegate introduces a paradigm, and the writing is slightly simplified:

Public delegate TResult MyFunc (T1 A1, T2 a2); public static int SomeMethod (string A1, bool a2) {return 0;} static void Main (string [] args) {MyFunc myFunc = SomeMethod;}

In .NET 2.0, new DelegateType can be omitted, and developers can assign methods directly to a reference to a delegate object. Of course, this improvement is not worth mentioning. The key to delegated writing in NET 2.0 is the introduction of "anonymous methods":

Public static void TestRequest (string url) {WebRequest request = HttpWebRequest.Create (url); request.BeginGetResponse (delegate (IAsyncResult ar) {using (WebResponse response = request.EndGetResponse (ar)) {Console.WriteLine ("{0}: {1}", url, response.ContentLength);}, null);}

An anonymous method is simply a delegate object inlined within the method, and its key is to form a closure (the context required for delegate execution). As in the above code, the * parameters (delegates) of BeginGetResponse can directly use the parameter url of the TestRequest method and the "local" variable request within the method. Without the feature of anonymous functions, the code would be troublesome to write, as you might have to do in .NET 1.x:

Public static void TestRequest (string url) {WebRequest request = HttpWebRequest.Create (url); object [] context = new object [] {url, request}; request.BeginGetResponse (TestAsyncCallback, context);} public static void TestAsyncCallback (IAsyncResult ar) {object [] context = (object []) ar.AsyncState; string url = (string) context [0]; WebRequest request = (WebRequest) context [1] Using (WebResponse response = request.EndGetResponse (ar)) {Console.WriteLine ("{0}: {1}", url, response.ContentLength);}}

At this point, we often find that developers need to spend a lot of effort maintaining a large piece of context for a small part of the code. For example, in this code, we cram the url and request objects into an object array, and then recover the data through a dangerous Cast operation in the callback function. If you want to be "strongly typed," you can only create a new context object for each callback, which can be more cumbersome to maintain-- you know, in parallel programming, asynchronous calls are becoming more and more important. without the feature that anonymous methods automatically retain context, developers will struggle with this "extra work".

You might say that anonymous methods are not readable because of the need for "inline". If there are too many inlines in a method, the maintenance cost goes up, so anonymous methods are not recommended. What I want to say is that you are wrong. If you want to separate methods for maintainability, you can also take advantage of anonymous methods:

Public static void TestRequest (string url) {WebRequest request = HttpWebRequest.Create (url); request.BeginGetResponse (delegate (IAsyncResult ar) {TestAsyncCallback (ar, request, url);}, null);} public static void TestAsyncCallback (IAsyncResult ar, WebRequest request, string url) {using (WebResponse response = request.EndGetResponse (ar)) {Console.WriteLine ("{0}: {1}", url, response.ContentLength);}}

With the help of the Lambda expression in .NET 3.5, the code can be written more easily and easily read:

Public static void TestRequest (string url) {WebRequest request = HttpWebRequest.Create (url); request.BeginGetResponse (ar = > TestAsyncCallback (ar, request, url), null); at this point, I believe you have a better understanding of how to write different versions of .NET delegation. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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: 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