1
執行for循環時,緩存了To之後的值。未緩存for循環的值
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之後的值。未緩存for循環的值
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
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僅在此時評估這些值。
感謝您的參考 –