1

执行 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 循环。

4

1 回答 1

1

没有办法让它评估不止一次。来自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 仅在此时计算这些值。

于 2013-10-29T16:27:22.490 回答