I. Generic delegates
The so-called generic delegate, that is, the parameters of a custom delegate can be constrained by generics, while the built-in delegate Func and the action itself are generic delegates.
Re-implement the methods in the Calculator class in the previous section with custom generic delegates.
1 Public classCalculator22 {3 //Traditional Solution One: Declare multiple methods in this class, namely, the method of doubling, square, cubic4 5 6 //Traditional Solution Two: Declare a universal method in this class, by passing different parameter types to distinguish whether to perform double or square or cubic operations7 8 9 //Solution Three: Declare a universal method, passing a delegate in, equivalent to passing a business logic, in the method only need to executeTen /// <summary> One ///Universal Method A /// </summary> - /// <param name= "Arrs" >an array of type int</param> - /// <param name= "Mydel" >Custom Delegate</param> the Public DelegateT mydel<t>(T t); - Public Static voidMyspecmethord<t> (t[] Arrs, mydel<t>Mydel) - { - for(inti =0; I < Arrs. Length; i++) + { -Arrs[i] =Mydel (Arrs[i]); + //Arrs[i] = Mydel. Invoke (Arrs[i]); //It's equivalent to the above sentence . A Console.WriteLine (Arrs[i]); at } - } - -}
Two. Built-in delegate
The. Net Framework provides two built-in delegates that support generics, namely action and Func, combined with lambda expressions, to improve development efficiency
The difference between the two:
1.Action can only delegate methods that have no return value, support 16 overloads (in represents an input parameter, and the overload has no return value)
respectively:
* Public delegate void Action<in t> (T obj);
* Public delegate void Action<in T1, in t2> (T1 arg1, T2 arg2);
* ......
* Public delegate void Action<in T1, in T2,......, in t16> (T1 arg1, T2 arg2,......, T16 arg16);
*
2.Func must delegate a method with a return value that supports 17 overloads (note that the last of the parentheses represents the return value, in represents the input parameter, and out represents the return value)
respectively:
* Public Delegate TResult Func<out tresult> ();
* Public delegate TResult Func<in T, out tresult> (t arg);
* Public delegate TResult Func<in T1, in T2, out tresult> (T1 arg1, T2 arg2);
* ......
* Public delegate TResult Func<in T1, in T2,....... on T16 out tresult> (T1 arg1, T2 arg2,...... T16 arg16);
*
Summary: In addition to the ref parameter and out parameters, the built-in delegate can basically be applied to any generic delegate scenario, very useful
3. The difference between the built-in delegate and the custom delegate:
A custom delegate needs to be declared to let the system know the custom delegate before the assignment method can be instantiated, and the built-in delegation system has the ability to do so, without having to declare it beforehand and instantiate the assignment method directly.
Three. Other properties of the Commission
Four. Events
. NET Advanced Series (8)-Delegates and Events (ii)