请看下面的代码:
Public Class TestClass
Public TestProperty As Integer
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim j As Integer
For j = 0 To 2
For i = 0 To 10
Dim k As Integer
Dim tc As TestClass
tc = New TestClass
tc.TestProperty = tc.TestProperty + 1
k = k + 1
Next
Next
End Sub
End Class
tc
在 FOR 循环的每次迭代中都会创建一个新对象(称为),因此tc.TestProperty
总是如此1
。为什么变量不是这种情况,k
即k
每次迭代时递增的值?我意识到这可能与如何处理值类型和引用类型有关,但我想检查一下。