执行for循环时,正在缓存To之后的值。
Dim m As Integer = 1
For x As Integer = 0 To m
Console.WriteLine(x)
m = 10
Next
输出
0
1
有没有办法不缓存m值?我能解决它的唯一方法是将For转换为While循环。
答案 0 :(得分:1)
无法对其进行多次评估。来自For..Next
Statement Documentation (Visual Basic) (MSDN)
For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
当
For
...Next
循环开始时,Visual Basic会评估开始,结束和步骤。 Visual Basic仅在此时评估这些值。