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 define and apply delegation in C #

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to define and apply the commission in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

I. Overview

Delegate is a reference type

2. Declare the delegate type and define the delegate public delegate void HandlerDelegate (string message); 2, declare the delegate variable HandlerDelegate myDelegate;// declare the delegate variable 3, instantiate the delegate with function reference

Traditional way

/ / myDelegate = new HandlerDelegate (Method1); / / can be a static method or an instance method. / / or directly myDelegate = Method1

Method definition:

Public void Method1 (string mgs) {Console.WriteLine (mgs);}

Or anonymous method:

MyDelegate = delegate (string message) / / instantiate delegate (with function reference) {Console.WriteLine (message);}

Or lambda expression:

MyDelegate = (message) = > {Console.WriteLine (message);}; 4. Invoke delegate myDelegate ("Hello World"); / / synchronous invocation III. Advanced usage 1. Multicast delegate public delegate void HandlerDelegate (string message); void Main () {HandlerDelegate myDelegate;// declares delegate variable myDelegate = (message) = > {Console.WriteLine ("a" + message);}; myDelegate + = (message) = > {Console.WriteLine ("b" + message);} MyDelegate + = (message) = > {Console.WriteLine ("c" + message);}; myDelegate ("Hello World"); / / multiple methods in turn call Console.WriteLine (myDelegate.GetInvocationList (). GetLength (0)); / / 3} 2, pass the delegate instance public delegate void HandlerDelegate (string message) as a parameter of the method; / / client void Main () {HandlerDelegate myDelegate / / declare the delegate variable myDelegate = (message) = > {Console.WriteLine ("calculation result:" + message);}; MyClass myClass = new MyClass (); myClass.MethodWithCallback (2, myDelegate);} / / as the server, return the result to the client public class MyClass {public void MethodWithCallback (int a, HandlerDelegate callback) {a = a + 1 after the calculation is complete. Callback ("result:" + a.ToString ()); / / execute delegate}} IV. Synchronous and asynchronous invocation 1. Synchronous invocation of delegate

The delegate's Invoke method is used for synchronous calls, which blocks the current thread, then executes the calling thread, and then resumes the main thread downward execution after the call is complete.

Public delegate int AddHandler (int a, int b); void Main () {AddHandler handler = (a, b) = > {return (a + b);}; / / int result = handler (2,3); int result = handler.Invoke (2,3); / / the following statement is executed after the method called in this sentence is executed. Console.Write (result); / / 5} 2. Asynchronous invocation of delegate

Put the call into the thread pool, and the program body thread or UI thread can execute at the same time.

Public delegate int AddHandler (int a, int b); void Main () {AddHandler handler = (a, b) = > {return (a + b);}; IAsyncResult ar = handler.BeginInvoke (2,3, null, null); / / while (! ar.IsCompleted) / / {/ / Console.WriteLine ("Do Sth."); / / the main thread can do other things before the delegated work is completed. / /} / / or while (! ar.AsyncWaitHandle.WaitOne (0, false)) {Console.WriteLine ("Do Sth.");} int result = handler.EndInvoke (ar); Console.Write (result); / / 5} 3, delegated asynchronous callback public delegate int AddHandler (int a, int b); void Main () {AddHandler handler = (a, b) = > {return (a + b);} IAsyncResult ar = handler.BeginInvoke (2,3, new AsyncCallback (AddComplete), null); Console.WriteLine ("Do Sth."); / / this method is called when the asynchronous call is complete. } public void AddComplete (IAsyncResult ar) {AddHandler handler = ((AsyncResult) ar). AsyncDelegate as AddHandler; Console.WriteLine (handler.EndInvoke (ar));} above is all the content of the article "how to define and apply delegation in C#". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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