我正在检查 VB.NET 中变量声明的位置是否无关紧要,除了范围,(对于这个问题),我想我最好检查一下当它们被“提升”到闭包中时会发生什么。我没有阅读规范,但我无法解释这些结果:
Dim outer As Integer
For i = 1 To 2
Dim inner As Integer
Try
Dim inner2 As Integer
Do
Dim inner3 As Integer
Call Sub()
Dim inner4 As Integer
Console.WriteLine(outer & ", " & inner & ", " & inner2 & ", " & inner3 & ", " & inner4)
outer = i
inner = i
inner2 = i
inner3 = i
inner4 = i
End Sub()
Loop Until True
Finally
End Try
Next
上述输出:
0, 0, 0, 0, 0
1, 1, 0, 1, 0
inner4
每次都重置是有道理的,其他所有或没有innerX
,但为什么只有inner2
?!