什么是循环变量闭包? [英] What is loop variable closure?

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

问题描述

我已阅读了在这个问题,在C# 5.0介绍了循环变量闭包,我访问了问题中的链接,找出它的含义。耻于我,我不能理解什么是循环变量闭包,因为博客文章解释了什么涉及从段块生成警告,之前没有生成任何编译警告,以及影响代码在新版本的编程语言中的变化。由于我的理解,循环闭包不是foreach的格式,而是数组中涉及的变量。这是正确的吗?

解决方案

取一个简单的 foreach p>

  foreach(var i in Enumerable.Range(0,10))
{
Console.WriteLine ); $ c $ b}
之前 C#5之前,只有



> 引入C#5之后,此循环现在将有10个单独的 i 变量...对于循环的每次迭代都有不同的变量。这些变量的生命周期较短,并且都具有相同的名称,但它们仍然是唯一的变量。 1

乍一看,



您可以看到与此代码示例的区别:

  var results = new List< Func< int>>(); 
foreach(var i in Enumerable.Range(0,10))
{
results.Add(()=> i);
}
foreach(结果中的var项目)
{
Console.WriteLine(item());
}

此代码将从VS 2010和VS 2012 )



A closure 是当编译器关闭或提升一个变量超出它的正常范围区域,并延长它的生命周期。每当你使用一个lambda表达式(由 => 运算符指示)时,通常也会涉及一个闭包。在我的示例中,变量 i 在闭包中使用,可用于放置在列表中的匿名函数。



了解变量和值之间的区别很重要。如果你可以运行VS2010中的代码,当只有一个变量时,区​​别很清楚...循环的早期迭代的变量中的值可以继续被循环的后续迭代修改,在函数叫做。






1 任何人问:没有性能差异。区别在于可能影响性能的编译器抽象详细信息,但没有任何可衡量的方式。


I have read in this question that in C# 5.0 was introduced loop variable closure, and I have accessed the link in the question to find out what it means. Shame on me, I wasn't able to understand much of what is a loop variable closure, since the blog article explains much about what involves to generate a warning from section blocks where before wasn't any compilation warning generated, and the impact of code changes at a new version of programming language. Since what I understand, the loop closure is not the format of foreach, but are the variables involved in array. Is this right?

解决方案

Take a simple foreach loop:

foreach(var i in Enumerable.Range(0,10))
{
    Console.WriteLine(i);
}

Before C# 5, there was just one variable i that was used for the entire run (every iteration) of the loop.

After the introduction of C# 5, this loop will now have 10 separate i variables... a different one for each iteration of the loop. These variables have shorter lifetimes and all have the same name, but they are still unique variables.1

At first glance the difference may not seem to matter, but when you're working with a closure the older behavior could have unexpected results.

You can see the difference with this code sample:

var results = new List<Func<int>>();
foreach(var i in Enumerable.Range(0,10))
{
    results.Add(() => i);
}
foreach(var item in results)
{
    Console.WriteLine(item());
}

This code will compile and run from both VS 2010 and VS 2012 (and later), but will give different results depending on which you used.

A closure is what happens when the compiler "closes over" or "hoists" a variable out of it's normal scope region and extends it's lifetime. Whenever you're working with a lambda expression (indicated by the => operator), there's very often a closure involved as well. In my sample, the variable i is used in a closure to be available to the anonymous functions placed in the list.

It's important to understand the distinction between the variable and value. If you can run the code in VS2010, when there was only one variable, the distinction is very clear... the value in the variable from earlier iterations of the loop can continue to be modified by later iterations of the loop, before the function is called.


1 Before anyone asks: there is no performance difference. The difference is a compiler abstraction detail that may affect performance, but no in any measurable way.

这篇关于什么是循环变量闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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