在c#中的lambda表达式示例
namespace ConsoleApplication2
{
delegate bool D();
delegate bool D2(int i);
class Test
{
D del;
D2 del2;
public void TestMethod(int input)
{
int j = 0;
del = () => { j = 10; return j > input; };
del2 = (x) => { return x == j; };
Console.WriteLine("j = {0}", j);
bool boolResult = del();
Console.WriteLine("j = {0}. b = {1}", j, boolResult);
}
static void Main()
{
Test test = new Test();
test.TestMethod(5);
bool result = test.del2(10);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
lambda表达式使程序更简单,lambda表达式是通过创建委托实现的匿名函数。我们可以通过函数调用的值传递局部函数。
日期:2020-04-11 23:03:44 来源:oir作者:oir
