Is there a reason for C#'s reuse of the variable in a foreach?

前端 未结 4 836
梦如初夏
梦如初夏 2020-11-21 22:27

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example:

foreach (var s          


        
相关标签:
4条回答
  • 2020-11-21 22:52

    Having been bitten by this, I have a habit of including locally defined variables in the innermost scope which I use to transfer to any closure. In your example:

    foreach (var s in strings)
        query = query.Where(i => i.Prop == s); // access to modified closure
    

    I do:

    foreach (var s in strings)
    {
        string search = s;
        query = query.Where(i => i.Prop == search); // New definition ensures unique per iteration.
    }        
    

    Once you have that habit, you can avoid it in the very rare case you actually intended to bind to the outer scopes. To be honest, I don't think I have ever done so.

    0 讨论(0)
  • 2020-11-21 22:55

    The compiler declares the variable in a way that makes it highly prone to an error that is often difficult to find and debug, while producing no perceivable benefits.

    Your criticism is entirely justified.

    I discuss this problem in detail here:

    Closing over the loop variable considered harmful

    Is there something you can do with foreach loops this way that you couldn't if they were compiled with an inner-scoped variable? or is this just an arbitrary choice that was made before anonymous methods and lambda expressions were available or common, and which hasn't been revised since then?

    The latter. The C# 1.0 specification actually did not say whether the loop variable was inside or outside the loop body, as it made no observable difference. When closure semantics were introduced in C# 2.0, the choice was made to put the loop variable outside the loop, consistent with the "for" loop.

    I think it is fair to say that all regret that decision. This is one of the worst "gotchas" in C#, and we are going to take the breaking change to fix it. In C# 5 the foreach loop variable will be logically inside the body of the loop, and therefore closures will get a fresh copy every time.

    The for loop will not be changed, and the change will not be "back ported" to previous versions of C#. You should therefore continue to be careful when using this idiom.

    0 讨论(0)
  • In C# 5.0, this problem is fixed and you can close over loop variables and get the results you expect.

    The language specification says:

    8.8.4 The foreach statement

    (...)

    A foreach statement of the form

    foreach (V v in x) embedded-statement
    

    is then expanded to:

    {
      E e = ((C)(x)).GetEnumerator();
      try {
          while (e.MoveNext()) {
              V v = (V)(T)e.Current;
              embedded-statement
          }
      }
      finally {
          … // Dispose e
      }
    }
    

    (...)

    The placement of v inside the while loop is important for how it is captured by any anonymous function occurring in the embedded-statement. For example:

    int[] values = { 7, 9, 13 };
    Action f = null;
    foreach (var value in values)
    {
        if (f == null) f = () => Console.WriteLine("First value: " + value);
    }
    f();
    

    If v was declared outside of the while loop, it would be shared among all iterations, and its value after the for loop would be the final value, 13, which is what the invocation of f would print. Instead, because each iteration has its own variable v, the one captured by f in the first iteration will continue to hold the value 7, which is what will be printed. (Note: earlier versions of C# declared v outside of the while loop.)

    0 讨论(0)
  • 2020-11-21 23:13

    What you are asking is thoroughly covered by Eric Lippert in his blog post Closing over the loop variable considered harmful and its sequel.

    For me, the most convincing argument is that having new variable in each iteration would be inconsistent with for(;;) style loop. Would you expect to have a new int i in each iteration of for (int i = 0; i < 10; i++)?

    The most common problem with this behavior is making a closure over iteration variable and it has an easy workaround:

    foreach (var s in strings)
    {
        var s_for_closure = s;
        query = query.Where(i => i.Prop == s_for_closure); // access to modified closure
    

    My blog post about this issue: Closure over foreach variable in C#.

    0 讨论(0)
提交回复
热议问题