A Lambda expression is an anonymous method that can contain an expression or statement block. It can create a delegate or expression tree type.
// Demo-a Lambda expression with a parameter
Namespace testlambda
{
// Declare a delegate that contains an int-type parameter
Delegate int del (int n );
Public class mylambda
{
Public int testmethod (int n)
{
/// Construct a delegate using lambda expressions
/// X => X * x expression indicates a method. This method is equivalent
/// Public int methodname (int x)
///{
/// Return x * X;
///}
/// From this we can see that, => X on the left represents the parameters of the anonymous method, and x * X on the right represents the method body.
/// This line of code can be summarized as: instantiate a delegate, which is used to calculate the square of the input integer Parameter
/// If you change the lambda expression to a method, the following line of code can be replaced:
/// Del MYDEL = new del (methodname );
Del MYDEL = x => X * X;
/// Call the delegate to call the lambda expression for computation.
Int result = MYDEL (N );
Return result;
}
}
Class Program
{
Static void main (string [] ARGs)
{
Mylambda my = new mylambda ();
/// Test the lambda expression
Int result = My. testmethod (10 );
Console. writeline (result );
/// The output result is 100.
}
}
}
// Demo2 -- a Lambda expression with two parameters and without any parameters
Namespace testlambda
{
/// <Summary>
/// Declare a delegate that contains two int-type parameters
/// </Summary>
/// <Param name = "N"> </param>
/// <Param name = "M"> </param>
/// <Returns> </returns>
Delegate int del (int n, int m );
/// <Summary>
/// Delegate without Parameters
/// </Summary>
/// <Returns> </returns>
Delegate int delnopar ();
Public class mylambda
{
Public int testmethod (int n, int m)
{
/// Construct a delegate using lambda expressions
/// The X and Y parameters of the lambda expression do not need to be declared in advance or specify the type. Of course, the X and Y parameters can also be specified.
/// For example: (int x, int y) => X * Y;
/// The complete expression is del MYDEL = (int x, int y) => X * Y;
Del MYDEL = (x, y) => X * Y;
/// Lambda expressions without any parameters
/// Use () to indicate null Parameters
/// This line of code indicates that the value of 10*5 is returned.
Delnopar testdel = () => 10*5;
Console. writeline (testdel ());
/// Call the delegate to call the lambda expression for computation.
Int result = MYDEL (n, m );
Return result;
}
}
Class Program
{
Static void main (string [] ARGs)
{
Mylambda my = new mylambda ();
/// Test the lambda expression
Int result = My. testmethod (10, 3 );
Console. writeline (result );
}
}
}
Summary:
1. Lambda expressions are an alternative to anonymous methods, but they are more flexible.
2. Lambda expressions, which can contain or do not contain parameters, must be expressed as () if they do not contain
3. Lambda expression parameters can be displayed with specified type or not specified. The program automatically analyzes and obtains the parameter type.