lambda表达式的要点是什么? [英] What's the point of a lambda expression?

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

问题描述

阅读这篇文章后,我无法弄明白为什么使用lambda表达式。为了公平起见,我不认为我对代理和表达式树类型有正确的了解,但是我不明白为什么有人会使用lambda表达式而不是声明的函数。有人可以启发我吗?

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);

当您可以只需要几百或几千个四行功能时,您的程序会出现什么问题?把你需要的代码 放在一个简短的表达式中。

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?

第二: lambdas关闭本地范围

您应该阅读,撰写和维护哪一个,这是:

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);

当您使用lambda时,所有令人烦恼的代码都是为您写的。

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

第三:查询理解被重写为lambdas for

当你写:

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.

第四:lambdas是可选的推断类型

Fourth: lambdas are optionally type-inferred

请注意,我们不必在上述查询理解或lambda版本中给出客户类型,但是我们必须在将其声明为静态方法时,给出形式参数的类型。编译器很聪明地从上下文中推断出lambda参数的类型。

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.

第五:Lambdas可以成为表达式树

Fifth: Lambdas can become expression trees

假设你想要一个网络服务器发送给你住在当前城市的客户的地址。你想(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.

表达式树允许编译器将lambda转换为可以转换为另一种查询格式的代码运行时并发送到服务器进行处理。在客户端运行的小帮手方法没有。

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表达式的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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