C # Common delegates

Source: Internet
Author: User

In the CLR environment, we have several commonly used delegate Action, action<t>, func<t>, Predicate<t>, and when we use the delegate, we try not to define a delegate ourselves. These are the ones built into the system that have been able to meet most of the requirements and have the code conform to specifications.

First, Action

The action encapsulated method has no parameters and no return value, and the declaration prototype is:

1 public delegate void Action ();

Usage is as follows:

public void Alert ()
 {
    Console.WriteLine ("This is a warning");
 }
    
 Action T = new action (Alert);  instantiate an action Delegate 
 T ();

If the statements in the delegate's method are shorter, 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>

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

Action<t> is a generic implementation of the action and no return value, but can pass in up to 16 parameters, and the declaration prototype of two parameters is:

1 public delegate void Action<in T1, in t2> (T1 arg1, T2 arg2);

Usage is as follows:

private void Showresult (int a, int b)
{
    Console.WriteLine (a + B);
}
   
Action<int, int> t = new Action<int, int> (Showresult);//two parameters but no return value of the delegate
T (2, 3);

You can also directly define a method directly in a delegate using the LAMBD expression, 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 argument, you can pass in a parameter, or you can pass in up to 16 parameters, but you can pass in up to 16 parameters, and two arguments a return value declaration prototype is:

1 public delegate TResult Func<in T1, in T2, out tresult> (T1 arg1, T2 arg2);

Usage is as follows:

public int Compare (int a, int b)
{return
    a > b;
}
   
Func<int, int, bool> t = new func<int, int, bool> (Compare);//pass in two int parameter, return bool result
= t (2, 3);

You can also directly define a method directly in a delegate using the LAMBD expression, as follows:

1 func<int, int, bool> t = (A, b) => {return a > b;};

2 bool result = T (2, 3);

Four, predicate<t>

The Predicate<t> delegate represents a method that defines a set of conditions and determines whether the specified object meets 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);

Usage is as follows:

public bool Match (int val)
{return
    val >
}
   
predicate<int> t = new predicate<int> (Match);   Define a comparison to entrust
int[] arr = {, 3, N,------------);            
INT-A-array.find (arr, t);                 Finds the first element in the array that is greater than 60

You can also directly define a method directly in a delegate using the LAMBD expression, as follows:

1 predicate<int> t = Val => {return val > 60;}; Define a comparison delegate

2 int[] arr = {13, 45, 26, 98, 3, 56, 72, 24};

3 INT-A-array.find (arr, t); Finds the first element in the array that is 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 return value, have parameter and have return value to think of func<t>

There are bool type return value, more in the comparator method, to entrust this method to think of using predicate<t>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.