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

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

问题描述

我正在寻找 C#7.0 ,我发现有趣的是他们已经实现了局部函数,但我无法想象一个局部函数比lambda表达式更受欢迎的情况,两者之间的差异。

我明白lambdas是 anonymous 函数,同时本地函数不是,但是我无法找出一个真实世界的场景,其中局部函数比lambda表达式有优势

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

解决方案

此由Mads Torgersen在C#设计会议笔记中进行了解释,其中首先讨论了本地函数

lockquote

您需要辅助函数。您只能在一个函数内使用它,并且它可能使用包含该函数的范围内的变量和类型参数。另一方面,与lambda不同,你并不需要它作为第一类对象,所以你不需要给它一个委托类型并分配一个实际的委托对象。您也许希望它是递归或通用的,或者将它作为一个迭代器来实现。


为了扩展它,优点是:
$ b


  1. 性能。



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



    另外,本地函数在捕获局部变量时效率更高:lambda通常捕获变量到类中,而本地函数可以使用一个struct(使用 ref )来传递,这又避免了分配。



    这也意味着调用本地函数更便宜,可以内联,可能会进一步提高性能。

  2. 本地函数可以递归。

    Lambdas也可以是递归的,但它需要笨拙的代码,首先将 null 分配给委托变量,然后分配lambda。局部函数自然可以是递归的(包括相互递归)。 本地函数可以是泛型的。 > Lambdas不能是泛型的,因为它们必须分配给一个具体类型的变量(该类型可以使用来自外部范围的泛型变量,但这不是一回事)。

  3. 本地函数可以实现为迭代器。



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


  4. 本地函数看起来更好。



    并可能只是我个人的偏见,但我认为正常的函数语法看起来比分配一个lambda到一个委托变量更好。本地功能也更简洁。



    比较:

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



I am looking to the new implementations in C# 7.0 and I found 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.

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.

解决方案

This was explained by Mads Torgersen in C# Design Meeting Notes where local functions were first discussed:

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. Performance.

    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.

    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.

  2. Local functions can be recursive.

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

  3. Local functions can be generic.

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

  4. Local functions can be implemented as an iterator.

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

  5. Local functions look better.

    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.

    Compare:

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

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

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