C# 循环中捕获的变量
我遇到了一个关于 C# 的有趣问题。 我有如下代码。
List<Func<int>> actions = new List<Func<int>>();
int variable = 0;
while (variable < 5)
{
actions.Add(() => variable * 2);
++ variable;
}
foreach (var act in actions)
{
Console.WriteLine(act.Invoke());
}
我期望它输出 0、2、4、6、8。然而,它实际上输出了 5 个 10。
看来这是由于所有操作都引用了一个捕获的变量。 因此,当它们被调用时,它们都有相同的输出。
有没有办法解决这个限制,让每个操作实例都有自己的捕获变量?
I met an interesting issue about C#. I have code like below.
List<Func<int>> actions = new List<Func<int>>();
int variable = 0;
while (variable < 5)
{
actions.Add(() => variable * 2);
++ variable;
}
foreach (var act in actions)
{
Console.WriteLine(act.Invoke());
}
I expect it to output 0, 2, 4, 6, 8. However, it actually outputs five 10s.
It seems that it is due to all actions referring to one captured variable. As a result, when they get invoked, they all have same output.
Is there a way to work round this limit to have each action instance have its own captured variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
正如其他人所说,它与循环无关。这是C#中匿名函数体中变量捕获机制的作用。
当您定义 lambda 作为示例时;
编译器为 lambda 函数生成一个容器类,如 <>c__DisplayClass0_0
() =>; ()=> 变量 * 2。
在生成的类(容器)内部,它生成一个名为 variable 的字段,其中包含一个具有相同名称的捕获变量和包含 lambda 主体的方法 b__0() 。
而名为 variable 的局部变量则成为容器类的一个字段 (<>c__DisplayClass0_0),
因此递增变量会依次递增容器类的字段,并且因为我们获得了容器类的一个实例while 循环的所有迭代,我们得到相同的输出,即 10。
您可以通过将循环体内捕获的变量重新分配给新的局部变量来防止
这种行为顺便说一句,这种行为仍然有效在 .Net 8 Preview 中,我发现这种行为非常错误且具有欺骗性。
As others said It has nothing to do with loops.It is the effect of the variable capture mechanism in the body of anonymous functions in C#.
When you define lambda as your example ;
Compiler generates a container class like <>c__DisplayClass0_0 for the lambda function
() => () => variable * 2.
Inside of the generated class(container) it generates a field named as variable, having a captured variable with the same name and the method b__0() containing the body of the lambda.
And than local variable named variable, becomes a field of a container class(<>c__DisplayClass0_0)
So incrementing variable results in turn incrementing field of container class and because we get one instance of the container class for all iterations of the while loop,we get same output that is 10.
You can prevent by reassigning captured variable inside the body of the loop to a new local variable
By the way,this behaviour still is valid with .Net 8 Preview and I find this behaviour is very buggy and deceptive.
这称为闭包问题,
只需使用复制变量即可。
It is called the closure problem,
simply use a copy variable, and it's done.
由于这里没有人直接引用 ECMA-334:
进一步在规范中,
哦,是的,我想应该提到的是,在 C++ 中不会出现此问题,因为您可以选择是否通过值或引用捕获变量(请参阅: Lambda 捕获)。
Since no one here directly quoted ECMA-334:
Further on in the spec,
Oh yea, I guess it should be mentioned that in C++ this problem doesn't occur because you can choose if the variable is captured by value or by reference (see: Lambda capture).
是的 - 在循环内获取变量的副本:
您可以将其视为 C# 编译器每次命中变量声明时都会创建一个“新”局部变量。 事实上,它会创建适当的新闭包对象,如果您引用多个作用域中的变量,它会变得复杂(就实现而言),但它可以工作:)
请注意,此问题更常见的情况是使用
for
或foreach
:有关此内容的更多详细信息,请参阅 C# 3.0 规范的第 7.14.4.2 节,以及我的 关于闭包的文章也有更多示例。
请注意,从 C# 5 编译器及更高版本开始(即使指定早期版本的 C#),
foreach
的行为发生了变化,因此您不再需要进行本地复制。 请参阅此回答以获取更多详细信息。Yes - take a copy of the variable inside the loop:
You can think of it as if the C# compiler creates a "new" local variable every time it hits the variable declaration. In fact it'll create appropriate new closure objects, and it gets complicated (in terms of implementation) if you refer to variables in multiple scopes, but it works :)
Note that a more common occurrence of this problem is using
for
orforeach
:See section 7.14.4.2 of the C# 3.0 spec for more details of this, and my article on closures has more examples too.
Note that as of the C# 5 compiler and beyond (even when specifying an earlier version of C#), the behavior of
foreach
changed so you no longer need to make local copy. See this answer for more details.我相信您正在经历的是所谓的闭包 http://en.wikipedia.org/wiki /Closure_(计算机科学)。 你的lambda有一个对作用域在函数本身之外的变量的引用。 你的amba在你调用它之前不会被解释,一旦它被解释,它将获得变量在执行时的值。
I believe what you are experiencing is something known as Closure http://en.wikipedia.org/wiki/Closure_(computer_science). Your lamba has a reference to a variable which is scoped outside the function itself. Your lamba is not interpreted until you invoke it and once it is it will get the value the variable has at execution time.
在幕后,编译器正在生成一个类来表示方法调用的闭包。 它为循环的每次迭代使用闭包类的单个实例。 代码看起来像这样,这使得更容易理解错误发生的原因:
这实际上不是示例中编译的代码,但我检查了我自己的代码,这看起来非常像编译器实际生成的代码。
Behind the scenes, the compiler is generating a class that represents the closure for your method call. It uses that single instance of the closure class for each iteration of the loop. The code looks something like this, which makes it easier to see why the bug happens:
This isn't actually the compiled code from your sample, but I've examined my own code and this looks very much like what the compiler would actually generate.
这与循环无关。
触发此行为是因为您使用 lambda 表达式
() => 变量 * 2
其中外部作用域变量 实际上并未在 lambda 的内部作用域中定义。Lambda 表达式(在 C#3+ 中,以及 C#2 中的匿名方法)仍然创建实际方法。 将变量传递给这些方法会遇到一些困境(按值传递?按引用传递?C# 采用引用 - 但这会带来另一个问题,即引用可能比实际变量更长寿)。 C# 解决所有这些困境的方法是创建一个新的辅助类(“闭包”),其中的字段对应于 lambda 表达式中使用的局部变量,方法对应于实际的 lambda 方法。 代码中对
variable
的任何更改实际上都会转换为ClosureClass.variable
中的更改,因此您的 while 循环会不断更新
ClosureClass.variable
直到它达到 10,然后 for 循环执行操作,这些操作都在同一个ClosureClass.variable
上运行。为了获得预期的结果,您需要在循环变量和正在关闭的变量之间创建分隔。 您可以通过引入另一个变量来做到这一点,即:
您还可以将闭包移动到另一个方法来创建这种分离:
您可以将 Mult 实现为 lambda 表达式(隐式闭包)
或使用实际的辅助类:
在任何情况下,“闭包”不是与循环相关的概念,而是与使用局部作用域变量的匿名方法/lambda 表达式相关 - 尽管一些不谨慎的循环使用表明了闭包陷阱。
This has nothing to do with loops.
This behavior is triggered because you use a lambda expression
() => variable * 2
where the outer scopedvariable
not actually defined in the lambda's inner scope.Lambda expressions (in C#3+, as well as anonymous methods in C#2) still create actual methods. Passing variables to these methods involve some dilemmas (pass by value? pass by reference? C# goes with by reference - but this opens another problem where the reference can outlive the actual variable). What C# does to resolve all these dilemmas is to create a new helper class ("closure") with fields corresponding to the local variables used in the lambda expressions, and methods corresponding to the actual lambda methods. Any changes to
variable
in your code is actually translated to change in thatClosureClass.variable
So your while loop keeps updating the
ClosureClass.variable
until it reaches 10, then you for loops executes the actions, which all operate on the sameClosureClass.variable
.To get your expected result, you need to create a separation between the loop variable, and the variable that is being closured. You can do this by introducing another variable, i.e.:
You could also move the closure to another method to create this separation:
You can implement Mult as a lambda expression (implicit closure)
or with an actual helper class:
In any case, "Closures" are NOT a concept related to loops, but rather to anonymous methods / lambda expressions use of local scoped variables - although some incautious use of loops demonstrate closures traps.
解决这个问题的方法是将您需要的值存储在代理变量中,并捕获该变量。
IE
The way around this is to store the value you need in a proxy variable, and have that variable get captured.
I.E.
是的,您需要在循环内确定变量的作用域并以这种方式将其传递给 lambda:
Yes you need to scope
variable
within the loop and pass it to the lambda that way:同样的情况也发生在多线程(C#,.NET 4.0]中。
请参阅以下内容code:
目的是按顺序打印 1,2,3,4,5
输出很有趣(可能像 21334...)
唯一的解决方案是使用局部变量。
The same situation is happening in multi-threading (C#, .NET 4.0].
See the following code:
Purpose is to print 1,2,3,4,5 in order.
The output is interesting! (It might be like 21334...)
The only solution is to use local variables.