Directory
Delegate
Action
Func
Summary
Delegate
Delegated tasks
The basic definition of delegation 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.
How to declare delegation: 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.
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}
Simple call:
1 static void main (string [] ARGs) 2 {3 say Sy = new say (chinesesay); // Method 4 say = chinesesay; // method 2 5 Sy ("hello"); 6 say ("hello"); 7 console. read (); 8}
As described above, it is a little troublesome to declare the delegate first and then use it in the general way of Delegate. the. NET has a defined delegate type, which can be used directly.
Action
There are two ways to delegate an Action: 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 = 3.5.0.0, culture = neutral, publickeytoken = b77a5c561934e089")] 4 Public Delegate void action ();
Depending on the number of input parameters, the action delegate has 16 overloading rules.
In the preceding example, the Delegate does not return a string type input parameter. Therefore, the generic version of the action is used.
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. endinvoke (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}
Result:
The code above lists the usage of the Action generic delegate and the LAMBDA Method. You can also use the anonymous method for the action and select a method you can use.
Func
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 parameters
Out: the output parameter, that is, the return value.
One case
Enter the name, age, gender, age, gender, and name.
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> funcupdateage = 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 = funcupdateage (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}
Result:
Func generic delegation, which can have no input parameters but must have output parameters.
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 ?)
[C # Basics] func and Action Learning