Lambda expression Learning Record

Source: Internet
Author: User

Lambda expressions can simplify some aspects of C # programming and have flexible usage. Therefore, it is not easy to master.

Below is a record of lambda expressions.

1. Lambda expressions are closely related to the delegate. Lambda expressions can be used whenever there is a delegate parameter type.

The Lambda expression operator is =>. The required parameters are listed on the left of the operator, and the implementation of the method for granting Lambda variables is defined on the right.Code. The following code is the simplest way to use it:

  public class   mylambda 
{
Public void display ()
{< br> string mid = ", middle part, ";
func string , string Lambda = Param =>
{< br> param + = mid;
param + = " and this was added to the string ";
return param;
};
console . writeline (lambda ( "Start of string" ));
}< BR >}< br>

Func<String,String> Is a delegate type. It contains two parameters: An input string and an output string.

Parm is an input parameter, so its type can be considered string type (of course, there are many types without names ).

Operator => the right side indicates a method. This method has no name. This method gives variable lambda.

In this exampleConsole. Writeline (lambda ("Start of string"); Pass the "Start of string" parameter to the lambda method. After processing, the output will be as follows:

Start of string, middle part, and this was added to the string.

So I want to understand it as follows:Lambda expressions are another way of writing a function. Operator => input on the left and function body on the right. The function body does not need to return values. It is determined by the receipt of the lambda expression defined by this function.

2. Lambda expression parameters.

If a Lambda expression has only one parameter, only the parameter name can be written. The following Lambda expression uses the parameter S because the delegate type defines a string type. So the type of S is string. The Code Implementing the function body calls the string. Format () method to return a string. When the delegate is called, the string is directly output:

Func<String,String> Oneparam = s =>
{
ReturnString. Format ("Change to uppercase {0 }", S. toupper ());
};

 
Console. Writeline (oneparam ("ABC"));
 
Note:For the usage of func, see the previous blog post. func <string, string> is a system-defined delegate with an input and an output.

If the delegate has multiple parameters, put the parameters in brackets as follows:

Func<Double,Double,Double> Twoparam = (x, y) =>
{
ReturnX * Y;
};

3. Single-line code and multi-line code

If a Lambda expression has only one statement, no curly brackets or return statements are required in the method block. The Compiler automatically adds an implicit return statement. For example, the two expressions above can be expressed:

 
Func<String,String> Oneparam = s =>String. Format ("Change to uppercase {0 }", S. toupper ());
Func<Double,Double,Double> Twoparam = (x, y) => X * Y;

However, if Lambda expressions require multiple statements to implement code, you must add curly braces and return statements. For example, the code in the first example.

4. external variables of lambda expressions

Lambda expressions can use external variables. However, you should pay attention to the following issues:

Let's take a look at the following code:

 
IntSomevar = 5;
Func<Int,Int> F = x => X + somevar;
Somevar = 10;
Console. Writeline (f (5 ));

Should the output be X + 5 or X + 10? The output is 15 (x + 10). After the external variable somevar is modified, the new value of the external variable is used.

For the expression x => X + somevar; the compiler creates an anonymous class, which has a constructor to pass external variables. The constructor depends on the number of variables passed from the outside. For the above example, we can think that the constructor accepts an int. An anonymous class contains an anonymous method, and its implementation code and return type are defined by lambda expressions:

Public classAnonymousclass
{
Private intSomevar;
PublicAnonymousclass (IntSomevar)
{
This. Somevar = somevar;
}
Public intAnonymousmethod (IntX)
{
ReturnX + somevar;
}
}

When lambda expressions are used, calling this method (anonymousmethod (int x) will create an instance of the anonymous class (equivalent to new anonymousclass (somevar )), and pass the value of the variable when the method is called.

Technorati label: Lambda

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.