执行 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 循环。
执行 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 循环。
没有办法让它评估不止一次。来自For..Next
声明文档 (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 计算 start、end 和 step。Visual Basic 仅在此时计算这些值。