Func<t> and action<t> are different from the description

Source: Internet
Author: User

I. Func

Func is a. NET built-in delegate.

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

    • Func<tresult>
    • Func<t,tresult>
    • Func<t1,t2,tresult>
    • Func<t1,t2,t3,tresult>
    • Func<t1,t2,t3,t4,tresult>

It has 5 forms, except that the number of parameters is different; the first one is no parameter, 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, at times, when we don't know what an interface is going to do at the same time, I can leave a delegate to it.

For more convenience,. NET has a delegate directly by default. Let's try it again. NET default with a delegate.

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

If you need a parameter, you can also send a copy of it.

        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 invocation 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 a return type;
    • Action<t> only the parameter type, it cannot be passed the return type. So action<t> 's delegate function has no return value.
Iv. Func and action support the form of lambda invocation

Or, after one input, returns the value that repeats once as an example.

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

Func<t> and action<t> are different from the description

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.