Visual Studio 2010
 
“Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式树类型。

x => x * x 读作“x goes to x times x”。可以将此表达式分配给委托类型,如下所示:

delegate int del(int i);
static void Main(string[] args)
{
del myDelegate
= x => x * x;
int j = myDelegate(5); //j = 25
}

实例:

using System.Linq.Expressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Expression
<del> myET = x => x * x;
}
}
}

=) 相同的优先级,并且是右结合运算符。

Where)的参数。

System.Func<T, TResult>

(以下几节中将对类型推理进行详细讨论。) 使用输入参数 5 调用委托时,它将返回结果 25。

as 运算符的左侧不允许使用 Lambda。

匿名方法(C# 编程指南)

Lambda 表达式


Lambda 表达式返回表达式的结果,并采用以下基本形式:
(input parameters) => expression

两个或更多输入参数由括在括号中的逗号分隔:

(x, y) => x == y

如果出现这种情况,您可以按以下示例中所示方式显式指定类型:

(int x, string s) => s.Length > x

使用空括号指定零个输入参数:

() => SomeMethod()

方法在 .NET 公共语言运行时上下文的外部将没有意义。

Lambda 语句


Lambda 语句与 Lambda 表达式类似,只是语句括在大括号中:
(input parameters) => {statement;}

Lambda 语句的主体可以包含任意数量的语句;但是,实际上通常不会多于两个或三个语句。

delegate void TestDelegate(string s); … TestDelegate myDel = n => { string s = n + " " + "World"; Console.WriteLine(s); }; myDel("Hello");

像匿名方法一样,Lambda 语句无法用于创建表达式树。

带有标准查询运算符的 Lambda


例如,假设有以下委托类型:

public delegate TResult Func<TArg0, TResult>(TArg0 arg0)

Func 委托时,该委托将返回 true 或 false 以指示输入参数是否等于 5:

Func<int, bool> myFunc = x => x == 5;
bool result = myFunc(4); // returns false of course

Expression<Func> 参数,Lambda 将编译为表达式树。

Count 方法:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);

n) 的数量,这些整数除以 2 时余数为 1。

numbers数组,因为这是不符合条件的序列中第一个数字是 9,左侧:

var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);

=>) 混淆。

var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);

Lambda 中的类型推理


Customer 对象,这意味着您可以访问其方法和属性:

customers.Where(c => c.City == "London");

Lambda 的一般规则如下:

  • Lambda 包含的参数数量必须与委托类型包含的参数数量相同。

  • Lambda 中的每个输入参数必须都能够隐式转换为其对应的委托参数。

  • Lambda 的返回值(如果有)必须能够隐式转换为委托的返回类型。

Expression 类型。

Lambda 表达式中的变量范围


下面的示例演示这些规则:
delegate bool D();
delegate bool D2(int i);

class Test
{
D del;
D2 del2;
public void TestMethod(int input)
{
int j = 0;
// Initialize the delegates with lambda expressions.
// Note access to 2 outer variables.
// del will be invoked within this method.
del = () => { j = 10; return j > input; };

// del2 will be invoked after TestMethod goes out of scope.
del2 = (x) => {return x == j; };

// Demonstrate value of j:
// Output: j = 0
// The delegate has not been invoked yet.
Console.WriteLine("j = {0}", j); // Invoke the delegate.
bool boolResult = del();

// Output: j = 10 b = True
Console.WriteLine("j = {0}. b = {1}", j, boolResult);
}

static void Main()
{
Test test
= new Test();
test.TestMethod(
5);

// Prove that del2 still has a copy of
// local variable j from TestMethod.
bool result = test.del2(10);

// Output: True
Console.WriteLine(result);

Console.ReadKey();
}
}

下列规则适用于 Lambda 表达式中的变量范围:

  • 捕获的变量将不会被作为垃圾回收,直至引用变量的委托超出范围为止。

  • 在外部方法中看不到 Lambda 表达式内引入的变量。

  • out 参数。

  • Lambda 表达式中的返回语句不会导致封闭方法返回。

  • continue 语句。

  

C# 语言规范


 有关更多信息,请参见 C# 语言规范。C# 语言规范是 C# 语法和用法的权威资料。

    相关文章: