title: Why do I get: "iteration variable in a lambda expression may have unexpected results"
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/227820/why-is-it-bad-to-use-a-iteration-variable-in-a-lambda-expression">Why is it bad to use a iteration variable in a lambda expression</a>  </p>
  </blockquote>
  
  
  
  <p>Why do I get: "iteration variable in a lambda expression may have unexpected results"? Suppose I have the following code:</p>
  
  <pre><code>  Dim writeAbleColumns As String() = {"IsSelected", "IsFeeExpense", "IsSubscriptionRedemption"}
    With grid
       For Each column As DataGridViewColumn In .Columns
        column.ReadOnly = Not Array.Exists(writeAbleColumns, Function(arrElement) column.Name = arrElement)
        Next
    End With
  </code></pre>
  
  <p>I get the warning:</p>
  
  <pre><code>Warning 1   Using the iteration variable in a lambda expression may have unexpected results.  Instead, create a local variable within the loop and assign it the value of the iteration variable.
  </code></pre>
  
  <p>I don't understand why changing my code to the following changes anything:</p>
  
  <pre><code>  Dim writeAbleColumns As String() = {"IsSelected", "IsFeeExpense", "IsSubscriptionRedemption"}
    With grid
       For Each column As DataGridViewColumn In .Columns
        Dim c As DataGridViewColumn = column
        column.ReadOnly = Not Array.Exists(writeAbleColumns, Function(arrElement) c.Name = arrElement)
        Next
    End With
  </code></pre>
  
  <p>Fundamentally nothing changes except the warning disappears. I just have another variable point to my variable. Why the warning? What unexpected things might happen?</p>