The CLR environment gives us built-in several commonly used delegate Action, action<t>, func<t>, PREDICATE<T>, generally we need to use the delegate, try not to define a delegate, The systems built in are already able to meet most of the requirements and let the code conform to the specifications.
First, Action
The action encapsulates a method that has no parameters and no return value, and the declaration prototype is:
1 public delegate void Action ();
Use the following:
1 public void Alert () 2 {3 Console.WriteLine ("This is a Warning"); 4 }5 6 Action T = new action (Alert); instantiate an action delegate of 7 T ();
If the statement in the delegate's method is short, you can also use the LAMBD expression to define the method directly in the delegate, as follows:
1 Action t = () = {Console.WriteLine ("This is a Warning");}; 2 T ();
Second, action<t>
Action<t> is a generic implementation of action, and there is no return value, but it can pass up to 16 parameters, and the declaration prototype for two parameters is:
1 public delegate void Action<in T1, in t2> (T1 arg1, T2 arg2);
Use the following:
1 private void Showresult (int a, int b) 2 {3 Console.WriteLine (A + b); 4}5 6 action<int, int> t = new Action<i NT, int> (Showresult);//two parameters but no return value of the delegate 7 T (2, 3);
It is also possible to directly define the method in the delegate directly with the LAMBD expression, the code is as follows:
1 Action<int, int> t = (A, b) = {Console.WriteLine (a + b);}; 2 T (2, 3);
Third, func<t>
The func<t> delegate always has a return value, the type of the return value is the last one in the parameter, you can pass in a parameter, you can pass up to 16 parameters, but you can pass in up to 16 parameters, and two parameters a declaration prototype of a return value:
1 public delegate TResult Func<in T1, in T2, out tresult> (T1 arg1, T2 arg2);
Use the following:
1 public bool Compare (int a, int b) 2 {3 return a > b;4}5 6 func<int, int, bool> t = new func<int, int, bo Ol> (Compare);//pass in two int parameter, return BOOL value 7 bool result = T (2, 3);
It is also possible to directly define the method in the delegate directly with the LAMBD expression, the code is as follows:
1 func<int, int, bool> t = (A, b) = = {return a > b;}; 2 bool result = T (2, 3);
Iv. predicate<t>
The Predicate<t> delegate represents a method that defines a set of conditions and determines whether the specified object conforms to these conditions, and the return value is always of type bool and the declaration prototype is:
1 public delegate bool Predicate<in t> (T obj);
Use the following:
1 public bool Match (int val) 2 {3 return val > 60;4}5 6 predicate<int> t = new predicate<int> (Match);
//define a comparison delegate 7 int[] arr = {3 ,, +, 98 8 int first = Array.find (arr, t); Find the first element in the array greater than 60
It is also possible to directly define the method in the delegate directly with the LAMBD expression, the code is as follows:
1 predicate<int> t = val = = {return val >;}; Define a comparison delegate 2 int[] arr = {$, 3 , $, 98 3 int first = Array.find (arr, t); Find the first element in the array greater than 60
Summarize:
- If the method you want to delegate has no parameters and no return value, think of the action
- With parameters but no return value, think of action<t>.
- No parameter has a return value, a parameter, and a return value to think of func<t>
- There is a bool type of return value, more used in the comparator method, to delegate this method to think of using predicate<t>
Transferred from: https://www.cnblogs.com/maitian-lf/p/3671782.html
C # delegate Action, action<t>, func<t>, predicate<t>