Lambda Expressions(Lambda 表达式)
(2012-10-25 23:41:19)
标签:
lambda |
分类: Programm |
class Test
{
}
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:
“Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式树类型。
所有 Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to”。 该 Lambda 运算符的左边是输入参数(如果有),右边包含表达式或语句块。 Lambda 表达式 x => x * x 读作“x goes to x times x”。可以将此表达式分配给委托类型,如下所示:
delegate int del(int
i);
static void Main(string[] args)
{
}
All restrictions that apply to anonymous methods also apply to lambda expressions.
适用于匿名方法的所有限制也适用于 Lambda 表达式。
(input parameters) => expression
(x, y) => x == y
(int x, string s) => s.Length
> x
() => SomeMethod()
Note in the previous example that the body of an expression
lambda can consist of a method call.However, if you are creating
expression trees that will be consumed in another domain, such as
SQL Server, you should not use method calls in lambda
expressions.The methods will have no meaning outside the context of
the .NET common language runtime.
在示例中,请注意 Lambda 表达式的主体可以包含方法调用。 但是,如果要创建将在另一个域(比如 SQL
Server)中使用的表达式树,则不应在 Lambda 表达式中使用方法调用。
方法在 .NET 公共语言运行时上下文的外部将没有意义。
(input parameters) =>
{statement;}
Func<int, bool> myFunc = x
=> x == 5;
bool result = myFunc(4);
// returns false of
course
int[] numbers = { 5, 4, 1, 3,
9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 ==
1);
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
var firstSmallNumbers = numbers.TakeWhile((n, index)
=> n >=
index);
The general rules for lambdas are as follows:
-
The lambda must contain the same number of parameters as the delegate type.
-
Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.
-
The return value of the lambda (if any) must be implicitly convertible to the delegate's return type.
Note that lambda expressions in themselves do not have a type because the common type system has no intrinsic concept of "lambda expression." However, it is sometimes convenient to speak informally of the "type" of a lambda expression. In these cases the type refers to the delegate type or Expression type to which the lambda expression is converted.
Lambda 的一般规则如下:
-
Lambda 包含的参数数量必须与委托类型包含的参数数量相同。
-
Lambda 中的每个输入参数必须都能够隐式转换为其对应的委托参数。
-
Lambda 的返回值(如果有)必须能够隐式转换为委托的返回类型。
请注意,Lambda 表达式本身没有类型,因为常规类型系统没有“Lambda 表达式”这一内部概念。但是,有时会不正式地论及 Lambda 表达式的“类型”。 在这些情况下,类型是指委托类型或 Lambda 表达式所转换为的 Expression 类型。
The following rules apply to variable scope in lambda expressions:
-
A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope.
-
Variables introduced within a lambda expression are not visible in the outer method.
-
A lambda expression cannot directly capture a ref or out parameter from an enclosing method.
-
A return statement in a lambda expression does not cause the enclosing method to return.
-
A lambda expression cannot contain a goto statement, break statement, or continue statement whose target is outside the body or in the body of a contained anonymous function.
下列规则适用于 Lambda 表达式中的变量范围:
-
捕获的变量将不会被作为垃圾回收,直至引用变量的委托超出范围为止。
-
在外部方法中看不到 Lambda 表达式内引入的变量。
-
Lambda 表达式无法从封闭方法中直接捕获 ref 或 out 参数。
-
Lambda 表达式中的返回语句不会导致封闭方法返回。
-
Lambda 表达式不能包含其目标位于所包含匿名函数主体外部或内部的 goto 语句、break 语句或 continue 语句。
Reference: