有什么特别之处倒闭? [英] What is so special about closures?

查看:113
本文介绍了有什么特别之处倒闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看完这篇文章关于闭包中,他们说:


  • 所有的管道自动

  • 编译器创建一个包装类和扩展了变量的生命

  • ,您可以使用局部变量不用担心

  • 的.NET编译器负责管道的你,等等。

  • "all the plumbing is automatic"
  • the compiler "creates a wrapper class" and "extends the life of the variables"
  • "you can use local variables without worry"
  • the .NET compiler takes care of the plumbing for you, etc.

所以我做了根据他们的代码,并给我一个例子,它好像刚刚封作用相似定期开展评选方法也照顾局部变量不用担心,并在所有的管道自动。

So I made an example based on their code and to me, it seems as though closures just act similarly to regular named methods which also "take care of the local variables without worry" and in which "all the plumbing is automatic".

或哪些问题会出现这种局部变量的包装解决,使关闭的特别之处/有趣/有用吗?

using System;
namespace TestingLambda2872
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int> AddToIt = AddToItClosure();

            Console.WriteLine("the result is {0}", AddToIt(3)); //returns 30
            Console.ReadLine();
        }

        public static Func<int, int> AddToItClosure()
        {
            int a = 27;
            Func<int, int> func = s => s + a;
            return func;
        }
    }
}





所以,这个问题的答案之一是上阅读乔恩斯基特的文章关闭的是马克指出。这篇文章不仅显示了在C#中导致对lambda表达式的演变也说明了闭包如何处理在Java中,对于这个主题的优秀的阅读。

Answer

So the answer to this one is to read Jon Skeet's article on closures that Marc pointed out. This article not only shows the evolution leading up to lambda expressions in C# but also shows how closures are dealt with in Java, an excellent read for this topic.

推荐答案

您的例子是不明确的,并且不(IMO)显示捕获的典型用法(拍摄的唯一的事情就是 A ,这始终是3 ,所以不是很有趣)

Your example isn't clear, and doesn't (IMO) show typical capture usage (the only thing captured is a, which is always 3, so not very interesting).

考虑这种教科书的例子(谓语):

Consider this text-book example (a predicate):

List<Person> people = ...
string nameToFind = ...
Person found = people.Find(person => person.Name == nameToFind);

现在尝试没有封闭;你需要做更多的工作,即使我们都懒的:

Now try it without a closure; you need to do a lot more work, even if we are lazy:

PersonFinder finder = new PersonFinder();
finder.nameToFind = ...
Person found = people.Find(finder.IsMatch);
...
class PersonFinder {
    public string nameToFind; // a public field to mirror the C# capture
    public bool IsMatch(Person person) {
        return person.Name == nameToFind;
    }
}



捕获方法进一步扩展到大量的变量在不同的范围 - 一个复杂性是隐藏的大量

除了名字,上面的C#编译器背后有什么近似场景。需要注意的是,当其他范围涉及我们开始链接不同的采集类(即内部范围必须捕获类外范围的参考)。相当复杂的。

Other than the names, the above is an approximation of what the C# compiler does behind the scenes. Note that when additional scopes are involved we start chaining the different capture classes (i.e. inner scopes have a reference to the capture class of outer scopes). Quite complex.

乔恩斯基特具有良好的文章就此这里,并在他的书更

Jon Skeet has a good article on this here, and more in his book.

这篇关于有什么特别之处倒闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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