Explain the difference between Funcless thanTgreater than and Actionless thanTgreater than in detail

  • 2021-08-21 20:13:19
  • OfStack

Func < T > , Action < T > Difference from description

1. Func

Func is a. Net built-in delegate.

Func < Result > , Func < T1,Result > Is a generic delegate built into. Net.


Func<TResult>
Func<T,TResult>
Func<T1,T2,TResult>
Func<T1,T2,T3,TResult>
Func<T1,T2,T3,T4,TResult>

It has five forms, but the number of parameters is different. The first is no parameter, but has a return value;

The following is an example of a simple ordinary delegate pass 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 a delegate for it.

For convenience,. Net has delegates directly by default. Let's try again. Net with delegates by default.


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

If parameters are needed, one copy can be transmitted in this way.


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();
}

2. Action

Action < T > The usage of Func is almost the same as that of 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();
}

3. Differences between Func and Action

Func and Action have almost the same effect. It's just

Func < Result > Have a return type;

Action < T > There are only parameter types, and no return types can be passed. So Action < T > None of the delegate functions of have a return value.

4. Both Func and Action support formal invocation of Lambda

Let's take the example of returning a value that repeats once after one input.


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

5. The most common places to see Func

Usually, we most often see Func in the parameters of methods as follows:

string XXX(Func<string, string>)

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

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

I see two points inside:

1. Extension method, independent of this article (IEnumerable is extended < TSource > To enable the implementation of IEnumerable < TSource > A collection of interfaces. Output function).

2. Func < TSource, int > selector.

Try writing an First function for Linq, named First2. Linq source code has a lot of exception handling, a lot of design patterns, unfortunately I do not understand, only extract simple logic.


namespace ConsoleApplication2
{
  static class Extend
  {
    public static TSource First2<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
      //.Net The source code itself a lot of abnormal handling, a lot of design patterns, I do not understand, only extract logic 
      foreach (TSource item in source)
      {
        if (predicate(item))
        {
          return (item);
        }
      }
      throw new Exception(" There is no article that satisfies the condition 1 Elements! ");
    }
  }
  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 whole content of this article, you can contact this site, thank you for the support of this site!


Related articles: