当使用封闭? [英] When to use closure?

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

问题描述

我已经看到了封样 - <一个href=\"http://stackoverflow.com/questions/36636/what-is-a-closure\">http://stackoverflow.com/questions/36636/what-is-a-closure

I have seen samples of closure from - http://stackoverflow.com/questions/36636/what-is-a-closure

任何人都可以提供何时使用封简单的例子?结果

Can anyone provide simple example of when to use closure?

具体来说,场景,其中封闭有道理?搜索

让我们假设该语言没有封闭的支持,怎么会仍然实现类似的事情?

Lets assume that the language doesn't have closure support, how would one still achieve similar thing?

不要得罪任何人,请在如C#,Python和JavaScript的,红宝石等结果的语言张贴code样品我很抱歉,我不明白的功能语言。

Not to offend anyone, please post code samples in a language like c#, python, javascript, ruby etc.
I am sorry, I do not understand functional languages yet.

推荐答案

闭包是很伟大的工具。何时使用它们?你喜欢的任何时间......正如已经表示,另一种方法是写一个类;例如,pre C#2.0中,创建一个参数化的线程是一个真正的斗争。在C#2.0,你甚至都不需要了`ParameterizedThreadStart'你只是做:

Closures are simply great tools. When to use them? Any time you like... As has already been said, the alternative is to write a class; for example, pre C# 2.0, creating a parameterised thread was a real struggle. With C# 2.0 you don't even need the `ParameterizedThreadStart' you just do:

string name = // blah
int value = // blah
new Thread((ThreadStart)delegate { DoWork(name, value);}); // or inline if short

与此相比,有一个名字和值

Compare that to creating a class with a name and value

或者同样用搜索列表(使用一个lambda这一次):

Or likewise with searching for a list (using a lambda this time):

Person person = list.Find(x=>x.Age > minAge && x.Region == region);

再次 - 另一种方法是写一个类具有两个属性和方法:

Again - the alternative would be to write a class with two properties and a method:

internal sealed class PersonFinder
{
    public PersonFinder(int minAge, string region)
    {
        this.minAge = minAge;
        this.region = region;
    }
    private readonly int minAge;
    private readonly string region;
    public bool IsMatch(Person person)
    {
        return person.Age > minAge && person.Region == region;
    }
}
...
Person person = list.Find(new PersonFinder(minAge,region).IsMatch);

这是相当的相媲美的编译器是如何做的引擎盖(实际上,它使用公共读取/写入领域,而不是私人只读)。

This is fairly comparable to how the compiler does it under the bonnet (actually, it uses public read/write fields, not private readonly).

用C#捕获的最大需要注意的是,观看范围;例如:

The biggest caveat with C# captures is to watch the scope; for example:

        for(int i = 0 ; i < 10 ; i++) {
            ThreadPool.QueueUserWorkItem(delegate
            {
                Console.WriteLine(i);
            });
        }

这可能无法打印您的期望,因为变量的用于每个我。你可以看到重复的任意组合 - 甚至是10的。你需要仔细范围捕获在C#中的变量:

This might not print what you expect, since the variable i is used for each. You could see any combination of repeats - even 10 10's. You need to carefully scope captured variables in C#:

        for(int i = 0 ; i < 10 ; i++) {
            int j = i;
            ThreadPool.QueueUserWorkItem(delegate
            {
                Console.WriteLine(j);
            });
        }

下面每个Ĵ被分开记录(即不同的编译器生成的类实例)。

Here each j gets captured separately (i.e. a different compiler-generated class instance).

乔恩斯基特具有良好的博客条目涵盖了C#和Java关闭的此处;或了解更多细节,请参见深度他的书 C#,这对他们整整一章。

Jon Skeet has a good blog entry covering C# and java closures here; or for more detail, see his book C# in Depth, which has an entire chapter on them.

这篇关于当使用封闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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