什么是一个lambda前pression的地步? [英] What's the point of a lambda expression?

查看:170
本文介绍了什么是一个lambda前pression的地步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读这篇文章,我想不通为什么拉姆达前pressions被使用过。为了公平起见,我不认为我有什么代表和前pression树类型是一个正确的认识,但我不明白为什么有人会用,而不是一个声明的函数的lambda前pression。有人可以告诉我吗?

After reading this article, I can't figure out why lambda expressions are ever used. To be fair, I don't think I have a proper understanding of what delegates and expression tree types are, but I don't understand why anyone would use a lambda expression instead of a declared function. Can someone enlighten me?

推荐答案

首先:简洁和地区

你更愿意写,阅读和维护?这样的:

Which would you rather write, read and maintain? This:

    var addresses = customers.Select(customer=>customer.Address);

static private Address GetAddress(Customer customer)
{
     return customer.Address;
}

... a thousand lines later ...

    var addresses = customers.Select(GetAddress);

什么是与数百或数千四线功能搞乱你的程序时,你可以只是把code你需要的点的在你需要它的为短前pression?

What's the point of cluttering up your program with hundreds or thousands of four-line functions when you could just put the code you need where you need it as a short expression?

二: lambda表达式接近对地方范围

你更愿意读,写和维护,这一点:

Which would you rather read, write and maintain, this:

var currentCity = GetCurrentCity();
var addresses = customers.Where(c=>c.City == currentCity).Select(c=>c.Address);

static private Address GetAddress(Customer customer)
{
     return customer.Address;
}

private class CityGetter
{
    public string currentCity;
    public bool DoesCityMatch(Customer customer)
    {
        return customer.City == this.currentCity;
    }
}

....

var currentCityGetter = new CityGetter();
currentCityGetter.currentCity = GetCurrentCity();
var addresses = customers.Where(currentCityGetter.DoesCityMatch).Select(GetAddress);

所有这些令人头痛的code是当您使用lambda写的。

All that vexing code is written for you when you use a lambda.

第三:为你的查询COM prehensions重写为lambda表达式

当你写的:

var addresses = from customer in customers
                where customer.City == currentCity 
                select customer.Address;

它转化为你的lambda语法。很多人觉得这种语法愉快的阅读,但我们需要的lambda语法,才能真正使其工作。

it is transformed into the lambda syntax for you. Many people find this syntax pleasant to read, but we need the lambda syntax in order to actually make it work.

第四: lambda表达式是可选类型推断

请注意,我们没有放弃在查询COM prehension客户的类型上面,还是在拉姆达版本,但我们确实有它声明为当给正式参数的类型一个静态方法。编译器是聪明的推断左右从上下文的lambda参数的类型。这使得你的code少多余的,更清晰。

Notice that we don't have to give the type of "customer" in the query comprehension above, or in the lambda versions, but we do have to give the type of the formal parameter when declaring it as a static method. The compiler is smart about inferring the type of a lambda parameter from context. This makes your code less redundant and more clear.

第五: Lambda表达式可以成为前pression树木

假设你要问一个Web服务器给我生活在当前城市的客户的地址。你想(1)从网站上拉下万客户,做客户端机器上的过滤,或(2)发送该网站的对象,告诉它查询包含在当前的城市,然后过滤器选择的地址的?让服务器做的工作,并寄给您只匹配的结果。

Suppose you want to ask a web server "send me the addresses of the customers that live in the current city." Do you want to (1) pull down a million customers from the web site and do the filtering on your client machine, or (2) send the web site an object that tells it "the query contains a filter on the current city and then a selection of the address"? Let the server do the work and send you only the result that match.

实施例pression树木允许编译器转动拉姆达到code,它可转化为在运行时另一个查询格式和发送到用于处理的服务器。在客户端上运行的小帮手方法没有。

Expression trees allow the compiler to turn the lambda into code that can be transformed into another query format at runtime and sent to a server for processing. Little helper methods that run on the client do not.

这篇关于什么是一个lambda前pression的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆