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 use C # delegation

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use C# delegation, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use C# delegate. Let's take a look.

1. What is entrustment

In terms of data structure, a delegate is a user-defined type like a class.

A delegate is an abstraction of a method, which stores the addresses of a series of methods with the same signature and return type. When the delegate is called, all methods contained in the delegate are executed.

2. Definition of delegation

A delegate is a type, just as a class is a type. Like classes, delegate types must be declared before they are used to create variables and type objects.

The declaration prototype of the delegate is

Delegate ()

Example: public delegate void MyDelegate (int number); / / defines a delegate MyDelegate that registers a function that returns a void type and has an int as an argument.

3. Instantiation of delegation 3.1 use the new keyword

The prototype of delegate instantiation is

= new ()

Example: MyDelegate _ MyDelegate=new MyDelegate (CheckMod); / / instantiate the above MyDelegate delegate to _ MyDelegate with the function CheckMod

3.2 use anonymous methods

= delegate () {function body}

3.3.Use the Lambda expression class Program {/ / to declare the delegate delegate int MyDelegate (int x, int y); static void Main (string [] args) {/ / instantiate delegate / / 1, using the new keyword MyDelegate _ myDelegate = new MyDelegate (GetSum) / / 2. Use the anonymous method MyDelegate myDelegate = delegate (int x, int y) {return x + y;}; / / 3. Use the Lambda expression MyDelegate myDelegateLambda = (int x, int y) = > {return x + y;} } static int GetSum (int x, int y) {return x + y;}} 4, generic delegation

Delegates also support the use of generics

Generic delegate prototype:

Delegate (T1 T1, T2 T2, T2 T3, T3...)

For example: delegate T2 DelegateDemo (T1 t); / / defines a delegate with two generics (T1 and T2), T2 as the delegate function return type and T1 as the delegate function parameter type

Static boo Check (int I) {if (I% 2% delegate 0) {return true;} return false;} static void Main (string [] args) {DelegateDemo _ delegate = Check;// instantiates a generic delegate to, that is, a function that has an int type parameter and the return type is bool. Console.WriteLine (_ delegate (9)); / / false} 5, C # built-in generic delegation

There are three built-in generic delegates in C #

Namespace DelegateDemo {class Program {/ / declare delegate delegate int MyDelegate (int x, int y); static void Main (string [] args) {/ / 1, Action can only delegate methods Action _ action = new Action (SayHello) that must have no return value; _ action ("Hello World") / / 2. Fun is just the method Func _ func = new Func (Check); _ func (5); / / 3, Predicate: this delegate returns a Bool value, which usually refers to a "judgment condition function". It should be pointed out that the judgment condition is generally "external hard condition", such as "greater than 50", rather than specified by the data itself, such as "it is not appropriate to find the largest element in the array." Predicate _ predicate = new Predicate (Check); / / use the Lambda expression Predicate predicate = p = > p% 2 = = 0; _ predicate (26);} static void SayHello (string strMsg) {Console.WriteLine (strMsg) } / / the return value is static bool Check (int I) {if (I% 2 = = 0) {return true;} return false;}} 6, Multicast delegation

When instantiating a delegate, a matching function must be registered with the delegate to instantiate a delegate object, but an instantiated delegate can register not only one function but also multiple functions. After registering multiple functions, each registration function is executed according to the registration order of the registration function.

The prototype of the delegate registered with the function:

+ = new ()

For example: MyDelegate _ myDelegate+=new MyDelegate (CheckMod); / / register the function CheckMod to the delegate instance _ checkDelegate

Starting with. Net 2.0, you can register matching functions directly with instantiated delegates:

+ =

For example: MyDelegate _ myDelegate+=CheckMod;// registers the function CheckMod with the delegate instance _ myDelegate

Note: the delegate must be instantiated before you can register other methods with + =. If you re-assign a value to a delegate instance with a registered function using the = sign, it is tantamount to re-instantiating the delegate, and there is no relationship between the previously registered function and the delegate instance.

There are + = register function to delegate, and-= unregister

For example: MyDelegate _ myDelegate-=CheckMod

If the delegate has a return value after registering multiple functions, then when the delegate is called, the return value of the last registered function will be returned.

Namespace DelegateDemo {class Program {/ / statement entrusts delegate int MyDelegate (int x, int y); static void Main (string [] args) {MyDelegate _ myDelegate = new MyDelegate (fun1); _ myDelegate + = fun2; Console.WriteLine (_ myDelegate (10Lab 23)); Console.ReadKey () / / output 10, returning the return value of the last registered function} static int fun1 (int x, int y) {return x + y;} static int fun2 (int x, int y) {return x;} this is the end of the article on "how to use C # delegation". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use C# delegation". If you still want to learn more knowledge, you are welcome to follow 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: 260

*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