C #3.0 new features (3) -- lambda expressions

Source: Internet
Author: User

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.

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.