【发布时间】:2012-03-01 21:03:17
【问题描述】:
可能重复:
Why is it bad to use a iteration variable in a lambda expression
为什么我会得到:“lambda 表达式中的迭代变量可能会产生意外结果”?假设我有以下代码:
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
我收到警告:
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.
我不明白为什么将我的代码更改为以下内容会发生任何变化:
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
除了警告消失之外,基本上没有任何变化。我只有另一个变量指向我的变量。为什么要发出警告?可能会发生什么意想不到的事情?
【问题讨论】: