C# 闭包未按预期工作

本文关键字:工作 闭包 | 更新日期: 2023-09-27 18:33:41

我不能清楚地理解两个代码块之间的区别。考虑有一个程序

    class Program
{
    static void Main(string[] args)
    {
        List<Number> numbers = new List<Number>
                                   {
                                       new Number(1),
                                       new Number(2),
                                       new Number(3)
                                   };

        List<Action> actions = new List<Action>();
        foreach (Number numb in numbers)
        {
            actions.Add(() => WriteNumber(numb));
        }
        Number number = null;
        IEnumerator<Number> enumerator = numbers.GetEnumerator();
        while (enumerator.MoveNext())
        {
            number = enumerator.Current;
            actions.Add(() => WriteNumber(number));
        }
        foreach (Action action in actions)
        {
            action();
        }
        Console.ReadKey();

    }
    public static void WriteNumber(Number num)
    {
        Console.WriteLine(num.Value);
    }
    public class Number
    {
        public int Value;
        public Number(int i)
        {
            this.Value = i;
        }
    }
}

输出为

1
2
3
3
3
3    

这两个代码块的工作方式应该相同。但是您可以看到闭包不适用于第一个循环。我错过了什么?

提前谢谢。

C# 闭包未按预期工作

在 while 循环之外声明number变量。对于每个数字,您将其引用存储在number变量中 - 每次都会覆盖最后一个值。

您应该将声明移动到 while-loop 内,以便每个数字都有一个新变量。

    IEnumerator<Number> enumerator = numbers.GetEnumerator();
    while (enumerator.MoveNext())
    {
        Number number = enumerator.Current;
        actions.Add(() => WriteNumber(number));
    }

这两个代码块的工作方式应该相同。

不,他们不应该 - 至少在 C# 5 中。事实上,在 C# 3 和 4 中,他们会这样做

但在 C# 5 的foreach循环中,每次循环迭代都有一个变量。您的 lambda 表达式捕获该变量。循环的后续迭代会创建不同的变量,这些变量不会影响先前捕获的变量。

while循环中,您有一个所有迭代捕获的变量。对该变量的更改将在捕获它的所有委托中看到。您可以通过在while循环后添加以下行来查看这一点:

number = new Number(999);

那么你的输出将是

1
2 
3
999
999
999

现在在 C# 3 和 4 中,foreach规范基本上被设计破坏了 - 它将在所有迭代中捕获单个变量。然后在 C# 5 中修复了此问题,以便每次迭代使用单独的变量,这基本上是你总是希望使用这种代码的

在你的循环中:

    Number number = null;
    IEnumerator<Number> enumerator = numbers.GetEnumerator();
    while (enumerator.MoveNext())
    {
        number = enumerator.Current;
        actions.Add(() => WriteNumber(number));
    }

数字在循环范围之外声明。 因此,当它设置为下一个当前迭代器时,所有对数字的引用操作也会更新为最新版本。 因此,当您运行每个操作时,它们都将使用最后一个数字。

感谢您的所有回答。但我想我被误解了。我希望闭门锁起作用。这就是我将循环变量设置在范围之外的原因。问题是:为什么在第一种情况下它不起作用?我忘了提到我使用 C# 3.5(不是 C# 5.0(。因此,soop 变量应该在范围之外定义,并且两个代码块的工作方式相同。