Using action, func, and lambda expressions
In. NET, we often use delegates, the role of delegates needless to say, before. NET 2.0, before we use delegates, we have to customize a delegate type, and then use this custom delegate type to define a delegate field or variable.. NET 2.0 brings US action, Func two generic delegate,. NET3.0 brought us lambda, all of which made the definition and use of the delegate simple. The delegates in the following example use lambda expressions.
I. Generic delegate for action series
The delegate of the Action series defines a delegate that has no return value (the return value is void). It has several versions including no input parameters, 1 input parameters, 2 input parameters, 3 input parameters, 4 input parameters, and a total of 5 versions of these versions of the prototype are as follows:
1. No input parameter returns a delegate with a value of void.
The action delegate encapsulates a method that takes no parameters and does not return a value.
You can use this delegate as a parameter to pass a method that performs an action without explicitly declaring a custom delegate to encapsulate the method. The encapsulated method must correspond to the method signature defined by this delegate. This means that the method must not have parameters and return values. Cases:
Using System;
Using System.Windows.Forms;
public class Name
{
private string InstanceName;
Public Action ShowName;
Public Show ()
{
If (ShowName! = null)
ShowName ();
}
Public name (string name)
{
This.instancename = name;
}
public void Displaytoconsole ()
{
Console.WriteLine (This.instancename);
}
public void Displaytowindow ()
{
MessageBox.Show (This.instancename);
}
}
public class Actionstudy
{
public static void Main ()
{
Name testname = new name ("Koani");
Testname.showname = () = Testname.displaytowindow ();
Testname.show ();
}
}
2.1 Input parameters return a delegate with a value of void
The action<t> generic delegate encapsulates a method that takes only one parameter and does not return a value.
You can use this delegate to pass a method as an argument without explicitly declaring a custom delegate. The method must be associated with this
The method signature that the delegate defines corresponds to. In other words, the encapsulated method must have a parameter passed to it by value, and the value cannot be returned. Cases:
Using System;
Using System.Windows.Forms;
public class Actionstudy
{
public static void Main ()
{
Action<string> Messagetarget;
if (Environment.getcommandlineargs (). Length > 1)
Messagetarget = s = = MessageBox.Show (s);
Else
Messagetarget = s = = Console.WriteLine (s);
Messagetarget ("Hello, world!");
}
}
The following example shows how to use the Action(t) delegate to print the contents of a List (t) object. In this example, the Print method is used to display the contents of the list to the console. In addition, the C # sample demonstrates how to display content to the console using anonymous methods.
Using System;
Using System.Collections.Generic;
Class Program
{
static void Main ()
{
Action<string> Printinconsole = s = Console.WriteLine (s);
action<string> Printindialog = S=>messagebox.show (s);
list<string> names = new list<string> ();
Names. ADD ("Bruce");
Names. ADD ("Alfred");
Names. ADD ("Tim");
Names. ADD ("Richard");
Names. ForEach (Printinconsole);
Names. ForEach (Printindialog);
}
}
3.2 input parameters return a delegate with a value of void
Action<t1,t2> encapsulates a method that has two parameters and does not return a value.
You can use the Action(T1, T2) delegate to pass a method as an argument without explicitly declaring a custom delegate. The
method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have two parameters that are passed to it by value and cannot return a value.
Using System;
Using System.IO;
public class Actinstudy
{
public static void Main ()
{
String message1 = "The first line of a message.";
String message2 = "The second line of a message.";
Action<string, string> concat;
if (Environment.getcommandlineargs (). Length > 1)
Concat = (S1, s2) = =
{
StreamWriter writer = null;
Try
{
writer = new StreamWriter (Environment.getcommandlineargs () [1], false);
Writer. WriteLine ("{0}" N{1} ", S1, S2);
}
Catch
{
Console.WriteLine ("File write operation failed ...");
}
Finally
{
if (writer! = null) writer. Close ();
}
};
Else
Concat = (S1, s2) = Console.WriteLine ("{0}" N{1} ", S1, S2);
Concat (Message1, Message2);
}
4.3 Input parameters return a delegate with a value of void
Action<t1,t2,t3> a delegate that encapsulates a method that takes three parameters and does not return a value.
You can use the Action(T1, T2, T3) delegate to pass the method as a parameter without explicitly declaring a custom delegate.
The method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have three parameters that are passed to it by value and cannot return a value.
5.4 Input parameters return a delegate with a value of void
Action<t1,t2,t3,t4> a delegate that encapsulates a method that has four parameters and does not return a value.
You can use the Action(T1, T2, T3, T4) delegate to pass the method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have four parameters that are passed to it by value and cannot return a value.
Two. Generic delegate for the Func system
The delegate for the Func series defines a delegate that returns a value. It has several versions including no input parameters, 1 input parameters, 2 input parameters, 3 input parameters, 4 input parameters, and a total of 5 versions of these versions of the prototype are as follows:
1. No delegate with input parameter with return value (return value not void)
Func<tresult> encapsulates a method that does not have a parameter but returns the type value specified by the TResult parameter.
You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate. The
method must correspond to the method signature defined by this delegate. This means that the encapsulated method must not have parameters, but a value has to be returned.
2. A delegate with an input parameter with a return value (the return value is not void)
The func<t,tresult> encapsulates a method that has a parameter and returns the type value specified by the TResult parameter.
You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate. The method must correspond to the method signature defined by this delegate. In other words, the encapsulated method must have a parameter passed to it by value, and must return a value.
3. A delegate with two input parameters with a return value (the return value is not void)
The func<t1,t2,tresult> encapsulates a method that has a parameter and returns the type value specified by the TResult parameter.
You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate. The method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have two parameters that are passed to it by value, and must return a value
4. A delegate with three input parameters with a return value (the return value is not void)
The func<t1,t2,t3,tresut> encapsulates a method that has three parameters and returns the type value specified by the TResult parameter.
You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate. The method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have three parameters that are all passed to it by value, and must return a value.
5. A delegate with four input parameters with a return value (the return value is not void)
The func<t1,t2,t3,tresult> encapsulates a method that has four parameters and returns the type value specified by the TResult parameter.
You can use this delegate to construct a method that can be passed as a parameter without explicitly declaring a custom delegate. The method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have four parameters that are all passed to it by value, and must return a value.
Using action, func, and lambda expressions