/*
* Lambda表达式的方法
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
delegate int MyDelegate(int x,int y);
class Program
{
static void Main(string[] args)
{
MyDelegate d = (int a, int b) => a + b;
MyDelegate d2 = (int x, int y) => { if (x > y) return x; else return y; };
Console.WriteLine("求和为:{0}",d(2, 3));
int f=Convert.ToInt32(Console.ReadLine());
int g = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}和{1}较大的为:{2}",f,g,d2(f, g));
Console.ReadKey();
}
}
}
运行结果:
注意:
Lambda表达式的参数类型可以省略;
Lambda的主题可以是表达式可以是语句块。