[C # Basics] Func AND Action Learning

Source: Internet
Author: User

[C # Basics] Func AND Action Learning
The basic definition of delegated tasks is briefly introduced in this article a long time ago. Let's take a look back. The delegate is type-safe in c #. You can subscribe to one or more function pointers with the same signature method to declare the delegate method: delegate return value type delegate type name (parameter) for example: 1 delegate void Say (string strContent); if you want to use this delegate, you need a corresponding method. Copy code 1 // <summary> 2 // delegate the corresponding method 3 /// </summary> 4 // <param name = "strContent"> </param> 5 private static void ChineseSay (string strContent) 6 {7 Console. writeLine (strContent); 8} a simple call to copy the code: Copy code 1 static void Main (string [] args) 2 {3 Say sy = new Say (ChineseSay ); // method 1 4 Say = ChineseSay; // method 2 5 sy ("hello"); 6 say ("hello"); 7 Console. read (); 8} copy the code as described above. In normal cases, the delegate is usually used. First, the delegate is declared and then used. This is a little troublesome ,.. net has a defined delegate type, which can be used directly. There are two ways to delegate an ActionAction: A delegate with no parameters and no return values, and a wildcard delegate with at least 16 parameters without return values. 1 // Abstract: 2 // encapsulate a method that does not have parameters and does not return values. 3 [TypeForwardedFrom ("System. core, Version =, Culture = Neutral, PublicKeyToken = b77a5c561934e089 ")] 4 public delegate void Action (); Action generic delegation varies depending on the number of input parameters, in the preceding example, the delegate has sixteen reloads and uses the Action to rewrite the preceding example. In the preceding example, the Delegate does not return a string type input parameter, so the generic version of the Action is used. Copy code 1 class Program 2 {3 static void Main (string [] args) 4 {5 Action <string> SayHello = new Action <string> (ChineseSay ); // method 1 6 Action <string> SayName = ChineseSay; // method 2 7 Action <string> SayAge = s => Console. writeLine ("I'm {0} years old this year", s); // method 3 8 SayHello ("hello"); 9 SayName. invoke ("My name is Wolfy"); 10 IAsyncResult result = SayAge. beginInvoke ("27", CallBack, "female"); 11 if (result. isCompleted) 12 {13 SayAge. endIn Voke (result); 14} 15 Console. read (); 16} 17 18 private static void CallBack (IAsyncResult ar) 19 {20 Console. writeLine ("Introduction complete, forget, I {0}, I don't do it", ar. asyncState. toString ()); 21} 22 // <summary> 23 // delegate the corresponding method 24 /// </summary> 25 // <param name = "strContent"> </param> 26 private static void ChineseSay (string strContent) 27 {28 Console. writeLine (strContent); 29} 30} the code above lists the usage of Action generic delegation and Lambda. Action can also use callback. Name method, select a method you can use. If you want to use a delegate with input parameters and return values, the Func delegate will meet your requirements. Func generic delegation, which can be input without parameters, but must return values. There are 17 reloads based on the number of input parameters. In: input parameter out: output parameter, that is, the return value. In one case, enter the name, age, gender, age, gender, and name. Copy code 1 public class Person 2 {3 public string Name {set; get;} 4 public int Age {set; get;} 5 public bool Gender {set; get ;} 6 /// <summary> 7 /// rewrite the tostring method, convenient output result 8 /// </summary> 9 /// <returns> </returns> 10 public override string ToString () 11 {12 return Name + "\ t" + Age + "\ t" + Gender; 13} 14} 15 class Program16 {17 static void Main (string [] args) 18 {19 Func <Person, Person> funcUpdate Age = new Func <Person, Person> (UpdateAge); 20 Func <Person, Person> funcUpdateAge2 = UpdateAge; 21 Func <Person, Person> funcUpdateGender = (p1) =={ p1.Gender = false; return p1 ;}; // lambda expression method 22 Func <Person, Person> funUpdateName = delegate (Person p2) // anonymous method 23 {24 p2.Name = "Wolfy2"; 25 return p2; 26}; 27 Person p = new Person () {Name = "Wolfy", Age = 24, gender = true}; 28 Person result = funcUpdate Age (p); 29 Console. writeLine (result. toString (); 30 Console. writeLine (funcUpdateGender (p ). toString (); 31 Console. writeLine (funUpdateName (p ). toString (); 32 Console. read (); 33} 34 static Person UpdateAge (Person p) 35 {36 p. age = 25; 37 return p; 38} 39 40} Func generic delegate. No input parameter is allowed, but an output parameter is required. Summary Action: No parameter, no return value, and delegation. Action <T>: indicates a generic delegate. There is no return value. There are 16 overload values based on the number of input parameters. Func <out T>: There is no input parameter and a return value. Func <in T, out T>: contains input parameters and return values. Depending on the number of input parameters, there are 16 overload values. In actions and Func, Lambda and anonymous methods can be used to process internal logic. (It's so hot that people are very impatient. When you don't know what to do, it's better to calm down and check for missing items on the foundation. you can master one point. Don't always complain and get bored, even if you complain and get bored, the last thing you should do is yours. It will be there if you don't have to worry about it. In summer, the weather is so annoying. Are you not doing anything? Isn't it a pleasure to think about code ?)

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.