我遇到了一个有趣的案例,其中pass-by-reference似乎在VB.NET中不起作用.我在下面提供了一些示例代码供大家使用.任何人都可以解释这种现象 这是预期的,还是语言/编译器的错误?
我在这段代码中看到的是"After Increment"读数与"Before Increment"读数相同.
Public Class Wrapper
Public Property Value As Integer
End Class
Sub Main()
Dim rand As New Random()
Dim w As New Wrapper()
w.Value = rand.Next()
Console.WriteLine("Before Increment: {0}", w.Value)
Try
Increment(w.Value)
Catch ex As Exception
End Try
Console.WriteLine("After Increment: {0}", w.Value)
Console.ReadLine()
End Sub
Public Sub Increment(ByRef i As Integer)
i += 1
Throw New Exception()
End Sub
Run Code Online (Sandbox Code Playgroud)