lambda 表达式的意义何在? [英] What's the point of a lambda expression?

查看:21
本文介绍了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

当你写作时:

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 是可选的类型推断

请注意,我们不必在上面的查询理解或 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可以变成表达式树

假设您想询问网络服务器将居住在当前城市的客户的地址发送给我".您是否想 (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天全站免登陆