區別在「值類型」和「引用類型」之間。值類型定義爲「結構」(VB.NET)或「結構」(C#),而引用類型定義爲「類」。原始類型,如整數,雙精度和布爾值是值類型。數組是參考類型。正如@MattiasÅslund指出的那樣,無論是傳遞ByVal還是ByRef,都帶有引用類型,您總是傳遞一個指針。
如果您確實想要操作傳遞的數組,但將原始數組返回給調用過程,則需要將副本複製到新的本地聲明數組中。但是,如果傳遞非常大的數組,請注意開銷。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myOriginalArray As String() = New String() {"Hello", "to", "you", "Michael"}
ManipulateArray(myOriginalArray)
MessageBox.Show("myOriginalArray = " & myOriginalArray(0) & " " & myOriginalArray(1) & " " & myOriginalArray(2) & " " & myOriginalArray(3))
End Sub
Private Sub ManipulateArray(ByVal myOriginalArray As String())
Dim myCopyArray(myOriginalArray.GetUpperBound(0)) As String
myOriginalArray.CopyTo(myCopyArray, 0)
myCopyArray(3) = "Sarah"
MessageBox.Show("myCopyArray = " & myCopyArray(0) & " " & myCopyArray(1) & " " & myCopyArray(2) & " " & myCopyArray(3))
End Sub
該函數可以在傳遞'ByRef'時替換整個對象,謝謝。 –