您的位置:首页 > 编程语言 > C#

C#—Lambda表达式用法

2016-04-15 20:58 288 查看
/*
* 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的主题可以是表达式可以是语句块。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#