【发布时间】:2013-11-13 12:17:54
【问题描述】:
在下面的示例中,是否在函数 ByRef 或 ByVal 中都传递 List(T) 对象是否重要?
这是正确的,因为 List 是一个引用类型,所以即使我传递对象 ByVal,值也总是会发生变化。
在更新列表时,如果我在函数“ListChanged”中传递对象 byRef 会更好吗?
Public Class MyClass_
Public Sub TestMethod()
Dim List_1 As New List(Of Integer)()
Dim List_2 As New List(Of Integer)()
List_1.Add(100)
List_2.Add(50)
List_1 = ActualListNotChanged(List_1) '---101
List_2 = ListChanged(List_2) '---50,51
End Sub
Private Function ActualListNotChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
Dim nList As New List(Of Integer)()
For Each item As Integer In lst
If item <> 50 Then
nList.Add(101)
End If
Next item
Return nList
End Function
Private Function ListChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
lst.Add(51)
Return lst
End Function
End Class
【问题讨论】:
标签: vb.net