1. What is commission
From the perspective of data structure, delegates are a user-defined type just like classes.
Delegation is an abstraction of methods, which stores addresses of methods with the same signature and return type. When a delegate is called, all methods contained in the delegate will be executed.
2. Definition of delegation
Delegates are types, just as if classes are types. Like a class, a delegate type must be declared before it is used to create variables and type objects.
The declaration prototype of the delegation is
delegate <function return type> <delegate name> (<function parameter>)
Example: public delegate void MyDelegate(int number);//Defines a delegate MyDelegate, which can register a function that returns void type and has an int as a parameter.
3. Instantiation of delegates
3.1 Using the new keyword
The prototype of delegate instantiation is
<Delegate Type> <Instanced Name>=new <Delegate Type>(<Register Function>)
Example: MyDelegate _MyDelegate=new MyDelegate(CheckMod);//Instantiate the above MyDelegate delegate as _MyDelegate using the function CheckMod
3.2 Using anonymous methods
<Delegate type> <Instance name>=delegate(<function parameter>){function body};
3.3 Using Lambda Expressions
class Program { //Declare the delegation delegate int MyDelegate(int x, int y); static void Main(string[] args) { //Instantiated delegation //1. Use the new keyword MyDelegate _myDelegate = new MyDelegate(GetSum); //2. Use anonymous method MyDelegate myDelegate = delegate(int x, int y) { return x + y; }; //3. Use Lambda expression MyDelegate myDelegateLambda = (int x, int y) => { return x + y; }; } static int GetSum(int x, int y) { return x + y; } }
4. Generic Delegation
Delegation also supports the use of generics
Generic delegate prototype:
delegate <T1> <Delegate name><T1,T2,T3...> (T1 t1,T2 t2,T3 t3...)
For example: delegate T2 DelegateDemo<T1,T2>(T1 t);//Define a delegate with two generics (T1,T2), T2 as the return type of the delegate function, and T1 as the delegate function parameter type
static boo Check(int i) { if(i%2==0) { return true; } return false; } static void Main(string[] args) { DelegateDemo<int, bool> _delegate =Check;//Instantiate the generic delegate <T1,T2> to <int,bool>, which means there is a function with an int type parameter and the return type is bool. (_delegate(9));//false }
5. Built-in generic delegate for C#
There are 3 built-in generic delegates in C#
namespace DelegateDemo { class Program { //Declare the delegation delegate int MyDelegate(int x, int y); static void Main(string[] args) { //1. Action<T> can only delegate methods that must have no return value Action<string> _action = new Action<string>(SayHello); _action("Hello World"); //2. Fun<TResult> is just a method that must have a return value in the delegate Func<int, bool> _func = new Func<int, bool>(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 suitable to find the largest element in the array". Predicate<int> _predicate = new Predicate<int>(Check); //Use Lambda expression Predicate<int> predicate = p => p % 2 == 0; _predicate(26); } static void SayHello(string strMsg) { (strMsg); } //The return value is bool value static bool Check(int i) { if (i % 2 == 0) { return true; } return false; } } }
6. Multicast commission
When instantiating a delegate, a matching function must be registered to the delegate to instantiate a delegate object. However, an instantiated delegate can not only register a function but also multiple functions. After registering multiple functions, each registered function will be executed in sequence according to the order of registration of the registered function when executing the delegate.
Prototype of function registration delegate:
<Delegate Type> <Instanced Name>+=new <Delegate Type>(<Register Function>)
For example: MyDelegate _myDelegate+=new MyDelegate(CheckMod);//Register the function CheckMod to the delegate instance_checkDelegate
Starting from .net 2.0, you can directly register matching functions to instantiated delegates:
<Delegate Type> <Instance pseudonym>+=<Register Function>
For example: MyDelegate _myDelegate+=CheckMod;//Register the function CheckMod to the delegate instance_myDelegate
Note: Delegation must be instantiated before it can be registered with +=. If the delegate instance registered with the function is assigned again using the = sign, it is equivalent to re-instanced the delegate, and no relationship will arise between the function previously registered on it and the delegate instance.
There are +=Register functions to delegate, and there are also -=Unregister
For example: MyDelegate _myDelegate-=CheckMod;
If after multiple functions are registered by the delegate, if the delegate has a return value, the return value of the last registered function will be returned when the delegate is called.
namespace DelegateDemo { class Program { //Declare the delegation delegate int MyDelegate(int x, int y); static void Main(string[] args) { MyDelegate _myDelegate = new MyDelegate(fun1); _myDelegate += fun2; (_myDelegate(10,23)); ();//Output 10, return 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 all about this article about the usage of C# commissions. I hope it will be helpful to everyone's learning and I hope everyone will support me more.