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:
void Action ();
Use the following:
1 void Alert () {3 Console.WriteLine (" This is a warning "); } //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:
void action<in t2> (T1 arg1, T2 arg2);
Use the following:
1private void showresult ( int A, int b) 2 {3 Console.WriteLine (a + b); 4 }5 6 Action<int, int> t = New Action<int, int> (showresult); // Two arguments but not a 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> t = (A, b) = {Console.WriteLine (a + b);}; 2 T (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:
Delegate TResult func<out tresult> (T1 arg1, T2 arg2);
Use the following:
1PublicBOOL Compare (int A,Int b) 2 {3 return a > B; 4 }5 6 Func<int, int, bool> t = new func<int, int, bool> (Compare); // pass in two int parameters, return bool value 7 Span style= "COLOR: #0000ff" >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<return a > b;}; bool result = t (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:
BOOL predicate< int> (T obj);
Use the following:
1PublicBOOL Match (IntVal2{3Return val >60;4}5 6 predicate<int> t = new predicate<int> (Match); // Define a comparison delegate 7 int[ ] arr = {13, 45, 26, 98, 3, 56, 72, 24}; 8 int first = Array.find (arr, t); //
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 > 60;}; // Define a comparison delegate 2 int[] arr = {13, 45, 26, 98, 3, 56, Span style= "COLOR: #800080" >72, 24}; 3 int first = Array.find (arr, t); //
Summary:
- 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>
C # delegate Action, action<t>, func<t>, predicate<t>