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

Lambda 表达式

2018-04-02 16:40 141 查看

1.Lambda 表达式

Lambda 表达式(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。delegate int calculator(int x, int y); //委托类型
static void Main()
{
calculator cal = new calculator(Adding);
int He = cal(1, 1);
Console.Write(He);
}

/// <summary>
/// 加法
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static int Adding(int x, int y)
{
return x + y;
}匿名方法如下:delegate int calculator(int x, int y); //委托
static void Main()
{
calculator cal = delegate(int num1,int num2)
{
return num1 + num2;
};
int he = cal(1, 1);
Console.Write(he);
}下面我们来讲解Lambda表达式: 按照上边的加法,我们用Lambda表达式来实现,代码如下:delegate int calculator(int x, int y); //委托类型
static void Main()
{
calculator cal = (x, y) => x + y;//Lambda表达式,大家发现没有,代码一个比一个简洁
int he = cal(1, 1);
Console.Write(he);
}那么我们详细讲讲Lambda表达式:若要创建 Lambda 表达式,需要在 Lambda 运算符 => 左侧指定输入参数(如果有),然后在另一侧输入表达式或语句块。 例如,lambda 表达式 x => x * x 指定名为 x 的参数并返回 x 的平方值。 如上面的示例所示,你可以将此表达式分配给委托类型:"Lambda表达式"是一个特殊的匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。Lambda表达式x => x * x读作"x goes to x times x"。举几个简单的Lambda表达式,如下:delegate bool MyBol(int x, int y);
delegate bool MyBol_2(int x, string y);
delegate int calculator(int x, int y); //委托类型
delegate void VS();
static void Main()
{
MyBol Bol = (x, y) => x == y;
MyBol_2 Bol_2 = (x, s) => s.Length > x;
calculator C = (X, Y) => X * Y;
VS S = () => Console.Write("我是无参数Labada表达式");
//
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
//
List<People> people = LoadData();//初始化
IEnumerable<People> results = people.Where(delegate(People p) { return p.age > 20; });
}

private static List<People> LoadData()
{
List<People> people = new List<People>(); //创建泛型对象
People p1 = new People(21, "guojing"); //创建一个对象
People p2 = new People(21, "wujunmin"); //创建一个对象
People p3 = new People(20, "muqing"); //创建一个对象
People p4 = new People(23, "lupan"); //创建一个对象
people.Add(p1); //添加一个对象
people.Add(p2); //添加一个对象
people.Add(p3); //添加一个对象
people.Add(p4);
return people;
}

}

public class People
{
public int age { get; set; } //设置属性
public string name { get; set; } //设置属性
public People(int age, string name) //设置属性(构造函数构造)
{
this.age = age; //初始化属性值age
this.name = name; //初始化属性值name
}
}

Func<T>委托

 T 是参数类型,这是一个泛型类型的委托,用起来很方便的。 先上例子static void Main(string[] args)
{
Func<int, string> gwl = p => p + 10 + "--返回类型为string";
Console.WriteLine(gwl(10) + ""); //打印‘20--返回类型为string’,z对应参数b,p对应参数a
Console.ReadKey();
}说明:我们可以看到,这里的p为int 类型参数, 然而lambda主体返回的是string类型的。再上一个例子static void Main(string[] args)
{
Func<int, int, bool> gwl = (p, j) =>
{
if (p + j == 10)
{
return true;
}
return false;
};
Console.WriteLine(gwl(5,5) + ""); //打印‘True’,z对应参数b,p对应参数a
Console.ReadKey();
}说明:从这个例子,我们能看到,p为int类型,j为int类型,返回值为bool类型。至此,如果上边的内容都能看懂,那么Lambda也就没什么了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ASP.NET Lambda 马春海