本地函数与 Lambda C# 7.0 [英] Local function vs Lambda C# 7.0

查看:25
本文介绍了本地函数与 Lambda C# 7.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 C# 7.0,我发现他们实现了本地函数很有趣,但我无法想象本地函数比 lambda 表达式更受欢迎的场景,以及什么是两者的区别.

I am looking at the new implementations in C# 7.0 and I find it interesting that they have implemented local functions but I cannot imagine a scenario where a local function would be preferred over a lambda expression, and what is the difference between the two.

我确实明白 lambda 是 匿名 函数而局部函数不是,但我无法弄清楚一个真实的场景,其中局部函数比 lambda 表达式有优势

I do understand that lambdas are anonymous functions meanwhile local functions are not, but I can't figure out a real world scenario, where local function has advantages over lambda expressions

任何示例都将不胜感激.谢谢.

Any example would be much appreciated. Thanks.

推荐答案

这是由 Mads Torgersen 在 C# 设计会议笔记中解释的首次讨论局部函数的地方:

你想要一个辅助函数.您仅在单个函数中使用它,并且它可能使用在包含函数范围内的变量和类型参数.另一方面,与 lambda 不同,您不需要将其作为第一类对象,因此您不必在意给它一个委托类型并分配一个实际的委托对象.此外,您可能希望它是递归的或泛型的,或者将其实现为迭代器.

You want a helper function. You are only using it from within a single function, and it likely uses variables and type parameters that are in scope in that containing function. On the other hand, unlike a lambda you don't need it as a first class object, so you don't care to give it a delegate type and allocate an actual delegate object. Also you may want it to be recursive or generic, or to implement it as an iterator.

进一步扩展它,优点是:

To expand on it some more, the advantages are:

  1. 性能.

在创建 lambda 时,必须创建一个委托,在这种情况下这是不必要的分配.本地函数实际上只是函数,不需要委托.

When creating a lambda, a delegate has to be created, which is an unnecessary allocation in this case. Local functions are really just functions, no delegates are necessary.

此外,局部函数在捕获局部变量方面更有效:lambda 通常将变量捕获到类中,而局部函数可以使用结构(使用 ref 传递),这再次避免了分配.

Also, local functions are more efficient with capturing local variables: lambdas usually capture variables into a class, while local functions can use a struct (passed using ref), which again avoids an allocation.

这也意味着调用本地函数的成本更低,而且它们可以被内联,从而可能进一步提高性能.

This also means calling local functions is cheaper and they can be inlined, possibly increasing performance even further.

局部函数可以递归.

Lambdas 也可以递归,但它需要笨拙的代码,您首先将 null 分配给委托变量,然后分配给 lambda.局部函数自然可以递归(包括相互递归).

Lambdas can be recursive too, but it requires awkward code, where you first assign null to a delegate variable and then the lambda. Local functions can naturally be recursive (including mutually recursive).

局部函数可以是通用的.

Local functions can be generic.

Lambda 不能是泛型的,因为它们必须分配给具有具体类型的变量(该类型可以使用外部作用域中的泛型变量,但这不是一回事).

Lambdas cannot be generic, since they have to be assigned to a variable with a concrete type (that type can use generic variables from the outer scope, but that's not the same thing).

局部函数可以作为迭代器来实现.

Local functions can be implemented as an iterator.

Lambdas 不能使用 yield return(和 yield break)关键字来实现 IEnumerable 返回函数.本地函数可以.

Lambdas cannot use the yield return (and yield break) keyword to implement IEnumerable<T>-returning function. Local functions can.

本地函数看起来更好.

这在上面的引用中没有提到,可能只是我个人的偏见,但我认为普通函数语法看起来比将 lambda 分配给委托变量更好.局部函数也更简洁.

This is not mentioned in the above quote and might be just my personal bias, but I think that normal function syntax looks better than assigning a lambda to a delegate variable. Local functions are also more succinct.

比较:

int add(int x, int y) => x + y;
Func<int, int, int> add = (x, y) => x + y;

这篇关于本地函数与 Lambda C# 7.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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