<>C# entrust (Delegate)

stay C# In the process of learning , Of course, there's our spirit boy —— entrust A place in the world
This article introduces it C# Delegation in , This guy can include functions of the same kind

C# Delegation in (Delegate) be similar to C or C++ Pointer to function in . entrust (Delegate)
A reference type variable that contains a reference to a method . References can be changed at run time . A delegate is a structure used to store methods
entrust (Delegate) Especially used to implement events and callback methods . All commissions (Delegate) All derived from System.Delegate class .

<> Declaration and definition delegation are divided into 4 Three steps

* Declare a delegate ( Function pointer )
* Create delegate object
* Creating functions that conform to the delegate format ( Function pointed to by pointer )
* Assign a function name to a delegate
Tips: The delegate object cannot be executed if it is empty , Judge in advance before execution

The code example is as follows
// 1. Declare a delegate ( Function pointer ) delegate void delegateTest(int a,float b); // delegate
return type Delegate type name ( parameter list ): // 2. Create delegate object public delegateTest dT1; //3.
Creating functions that conform to the delegate format ( Function pointed to by pointer ) public void Test(int a,float b) { a+=100; b*=2.0; Console.
WriteLine("a:{0},b:{1}", a,b); } static void Main(string[] args) { // 4.
Assign a function name to a delegate dT1=new delegateTest(Test); // Use delegation dT1(1,1.0); // Print results //a=101,b=2.0 }
Delegation type

* entrust / agent : handle
* Delegate is a custom type
* A delegate is a reference type
Several basic entrustment structures

* Custom delegation grammar : delegate Return type Commission name ([ parameter ]);
* Action Built in delegation grammar : Action<[ Parameter type 0-16 individual ]> Commission name = Delegate method name ;
* Func Built in delegation grammar : Function<[ Parameter type 0-16 individual ], Return type > Commission name = Delegate method name ;
* Multicast delegation grammar : Commission name += Delegate method name ; Commission name -= Delegate method name ;
<> Usage of delegation

The following example demonstrates one use of delegation , You can use the defined delegate as a parameter , You can call the method in the delegate directly through the delegate .
// Declaration of entrustment public delegate void OndelegateTest(int a,int b); public static void
TestNum1(int i,int j) { int num = i+j; Console.WriteLine("num:"+num); } public
static void TestNum2(int i,int j) { int num = i*j; Console.WriteLine("num:"+num)
; } // The method takes the delegate as a parameter , And use it to call methods public static void sendNum(OndelegateTest test) {
test(1,2); } static void Main(string[] args) { OndelegateTest d1=new
OndelegateTest(TestNum1); OndelegateTest d2=new OndelegateTest(TestNum2);
sendNum(d1); sendNum(d2); Console.ReadKey(); }
<> Delegated multicast (Multicasting of a Delegate)

Delegate object can use “+” Operator . A merge delegate calls the two delegates it merges . Only delegates of the same type can be merged ."-" Operators can be used to remove a component delegate from a merged delegate .

Use this useful feature of delegation , You can create a call list of the methods to be called when the delegate is called . This is called delegated
Multicast (multicasting), Also called multicast . The following program demonstrates the multicast of delegation :
using System; // Declaration of entrustment delegate int NumberChanger(int n); namespace DelegateAppl
{ class TestDelegate { static int num = 10; public static int AddNum(int p) {
num+= p; return num; } public static int MultNum(int q) { num *= q; return num;
} public static int TestNum(int i) { num -= i; return num; } public static int
getNum() { return num; } static void Main(string[] args) { // Create delegate instance
NumberChanger nc; NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger
nc2= new NumberChanger(MultNum); nc = nc1; nc += nc2; nc += TestNum; // Call multicast nc(
5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } }
When the above code is compiled and executed , It produces the following results :

Value of Num: 70

Binding and unbinding of delegate objects
delegate int NumberChanger(int n); NumberChanger nc; public static int TestNum(
int i) { num -= i; return num; } nc += TestNum;// Binding of delegation nc -= TestNum;// Debinding of delegation

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?