Differences between Func & lt; T & gt; and Action & lt; T & gt;: funcaction

Source: Internet
Author: User

Detailed description of the difference between Func and Action, funcaction

Differences between Func and Action

I. Func

Func is A. Net built-in delegate.

Func <Result>, Func <T1, Result> is a. Net built-in generic delegate.

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 that there is no parameter, but there is a return value;

The following is an example of a simple normal delegate to transfer methods.

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 operations an interface is doing at the same time, I can leave a delegate for it.

For convenience,. Net directly has a delegate by default. Let's try again. Net's default 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 upload them 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 ();}

Ii. Action

The usage of Action <T> is almost the same as that of Func, and the call method is similar.

ActionAction<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. Differences between Func and Action

Func works almost the same as Action. Just

Func <Result> has a return type;

Action <T> only the parameter type cannot be input. Therefore, the delegate functions of Action <T> do not return values.

4. Both Func AND Action support Lambda calls.

Return the repeated value after an input as an example.

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

5. Most Common Func scenarios

Generally, Func is most common in the method parameters 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);

There are two points:

1. The extension method is irrelevant to this article (the extension is IEnumerable <TSource>, mainly to implement the set of IEnumerable <TSource> interfaces. Output Functions ).

2. Func <TSource, int> selector.

Try to write a function named first2. There are many exception handling and many design patterns in the source code of Linq. Unfortunately, I don't understand them, but I only extract simple logic.

Namespace ConsoleApplication2 {static class Extend {public static TSource First2 <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate ){//.. Net source code, exception handling, many design patterns, I don't understand, just extract the logic foreach (TSource item in source) {if (predicate (item )) {return (item) ;}} throw new Exception ("the first element that meets the condition does not exist! ") ;}} 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 all the content of this article. If you have any questions, please contact the editor. Thank you for your support for the help house!

Related Article

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.