Detailed func<t> and action<t> difference _ practical skills

Source: Internet
Author: User
Tags exception handling static class

The difference between func<t> and action<t> is explained

First, Func

Func is a. NET built in delegate.

Func<result>,func<t1,result> is a. NET-built generic delegate.

func<tresult>
func<t,tresult>
func<t1,t2,tresult>
func<t1,t2,t3,tresult>
func<t1,t2,t3,t4,tresult>

It has 5 forms, but the number of parameters is different; the first one is without parameters, but there is a return value;

The following is an example of a simple generic delegate to pass a method.

Private delegate string Say ();
public static string SayHello ()
{return
  "Hello";
}
static void Main (string[] args)
{
  Say Say = SayHello;
  Console.WriteLine (Say ());
  Console.readkey ();
}

So, sometimes, when we don't know what an interface is going to do at the same time, I can leave it a delegate.

In order to be more convenient,. NET has a delegate directly by default. Let's try again. NET Default-band delegate.

public static string SayHello ()
{return
  "Hello";
}
static void Main (string[] args)
{
  func<string> say = SayHello;
  Console.WriteLine (Say ());
  Console.readkey ();
}

If you need parameters, you can also pass a copy.

public static string SayHello (String str)
{return
  str + str;
} 
static void Main (string[] args)
{
  func<string, string> say = SayHello;
  String str = Say ("abc");  
  Console.WriteLine (str);   Output ABCABC
  console.readkey ();
}

Second, Action

The use of action<t> is almost the same as Func, and the calling method is similar.

Action
action<t>
action<t1,t2>
action<t1,t2,t3>
action<t1,t2,t3,t4 >
Private delegate string Say ();
public static void SayHello (String str)
{
  Console.WriteLine (str);
}
static void Main (string[] args)
{
  action<string> say = SayHello;
  Say ("abc");
  Console.readkey ();
}

Iii. the difference between Func and action

Func is almost the same as action. It's just that

Func<result> has return type;

Action<t> only the parameter type and cannot pass the return type. So action<t> 's delegate function has no return value.

Four, Func and action support the form of a lambda call

Or, after one input, return the duplicate value as an example.

Func<string, string> say = m => m + m;
Console.WriteLine (Say ("abc")); Output ABCABC

V. The most common places to Func

Usually the most common to Func is in the parameters of the method as follows:

string XXX(Func<string, string>)

Let's take a look at one of the Sum in LINQ:

public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);

Inside see two points:

1, extension method, and this article is irrelevant (extended is ienumerable<tsource>, mainly to be able to implement the Ienumerable<tsource> interface of the collection.).

2, Func<tsource, int> selector this parameter.

Try to write a LINQ, named First2. LINQ source code has a lot of exception handling, many design patterns, unfortunately I do not understand, only the extraction of simple logic.

Namespace ConsoleApplication2
{
  static class Extend
  {public
    static TSource first2<tsource> ( This ienumerable<tsource> source, Func<tsource, bool> predicate)
    {
      //. NET own source code a lot of unusual situation processing, many design pattern, I also do not understand, only extracts the logic
      foreach (TSource item in Source)
      {
        if (predicate (item))
        { return
          (item);
        }
      throw new Exception ("No first element satisfies the condition!") ");
    }
  }
  Class program
  {
    static void Main (string[] args)
    {
      list<int> listint = new List<int> () { 1, 2, 3, 4, 5};
      int k = Listint.first2 (M => m > 4);   Output 5
      Console.WriteLine (k);
 
      Console.readkey ();}}

The above is the entire content of this article, there are problems can be linked with small, thank you for the support of the cloud-Habitat community!

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.