Func and action

Source: Internet
Author: User

We usually declare a delegate type if we want to use a delegate, for example:

Private Delegate string Say ();

The string describes the return type of the method that applies to the delegate, which has no arguments after the delegate name Say , indicating that the corresponding method has no incoming arguments.

Write a method that applies to the delegate:

      Public Static string SayHello ()        {            return"Hello";        }

Last Call:

       Static void Main (string[] args)        {            = SayHello;            Console.WriteLine (Say ());        }

Here we declare the delegate before passing the method to the delegate. Is there a way to not define a delegate variable?

The answer is yes, we can use Func.

Func is a built-in delegate inside. NET, which has many overloads.

func<TResult >: A delegate of type TResult is returned without an incoming parameter. Just like the say delegate above us, we can replace it with func<string> , which is called as follows:

      Static void Main (string[] args)        {            Func<string> Say = SayHello;             // Say Say = SayHello;             Console.WriteLine (Say ());        }

Well, it's easy to have Func . Take a look at the overloads of the Func .

func<t, TResult> Delegate: There is an incoming parameter T, which returns a delegate of type TResult . Such as:

     // delegate incoming parameter type is string, method return type int     func<stringint> A = Count;
      // Correspondence Method         Public int Count (string  num)        {            return  convert.toint32 (num);        }

func<T1, T2, TResult> Delegate: There are two incoming parameters:T1 and T2, The return type is TResult.

Similarly there are func (T1, T2, T3, TResult) delegates,func (T1, T2, T3, T4, TResult) delegates and so on. In the same way, it is the incoming parameter of the method, and the last is the return type of the method.

Func can also be used with anonymous methods such as:

 Public Static voidMain () {Func<string,int,string[]> Extractmeth =Delegate(stringSinti) {Char[] delimiters =New Char[] {' ' }; returni >0?S.split (delimiters, i): S.split (delimiters);            }; stringtitle ="The Scarlet Letter"; //Use Func instance to call Extractwords method and display result            foreach(stringWordinchExtractmeth (title,5) ) Console.WriteLine (word); }

It can also be connected to a lambda expression

   Public Static voidMain () {Char[] separators =New Char[] {' '}; Func<string,int,string[]> extract = (s, i) = =I>0?S.split (separators, i): S.split (separators); stringtitle ="The Scarlet Letter"; //Use Func instance to call Extractwords method and display result      foreach(stringWordinchExtract (title,5) ) Console.WriteLine (word); }

Func has a return type, what if our method does not have a return type? The Action is going to be on the stage when it's a clang.

Action Delegate: There is no incoming parameter, there is no return type, that is void. Such as:

       Static void Main (string[] args)        {            = SayHello;
say (); } Public Static void SayHello () { Console.WriteLine ("Say Hello"); }

action<T> Delegate: The incoming parameter is T, and there is no return type. Such as:

      Static void Main (string[] args)        {            Action<string> Say = SayHello;            Say ("Hello");        }          Public Static void SayHello (string  Word)        {            Console.WriteLine (word);        }

action<T1, T2> Delegate: Two incoming parameters, respectively T1 and T2, no return type.

The Action also has many other overloads, the same as each overloaded usage, except that the method has a different number of incoming parameters.

In fact, the action and the use of Func Almost, the difference is only a return type, a no return type, of course, the action can also be anonymous method and Lambda expression.

Anonymous method:

    Static void Main (string[] args)        {            Action<stringdelegate(string  Word)            {                Console.WriteLine (word);            };            Say ("Hello Word");        }

Lambda expression:

     Static void Main (string[] args)        {            Action<string> say = S = Console.WriteLine (s) ;            Say ("Hello Word");        }

Func and action

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.