嗨...曾经在 VB 中的数组中使用过 byval 和 byref 吗??...我传递了一个数组 byval 然后 byref ...在这两个过程中,ARRAY 更改都被带出程序..为什么?

问题描述 投票:0回答:1

请复制下面的粘贴程序并尝试... '************ 导入系统

模块程序 Sub inputproc(ByRef pstudarr()作为字符串) 昏暗计数为整数 对于计数 = 0 到 3 Console.WriteLine("输入姓名" & count) pstudarr(count) = Console.ReadLine 下一个 结束子

Sub display(ByRef pstudarr() As String)
    Dim count As Integer
    For count = 0 To 3

        Console.WriteLine(pstudarr(count))
        pstudarr(count) = "zzzzzzz"
    Next
End Sub
Sub Main(args As String())
    Dim studarr(3) As String
    Call inputproc(studarr)
    Call display(studarr)
    Console.ReadLine()
    Call display(studarr)
End Sub

结束模块 '***************

接下来替换子显示(ByRef pstudarr() As String)

与 子显示(Byval pstudarr() As String)

你会看到...它是相同的输出

所以它似乎与数组中的 BYVAL 和 BYREF 没有区别???

相同的输出...似乎 byval 和 byref 对数组没有区别。

有人有同样的问题吗???

arrays vb.net difference byref byval
1个回答
0
投票

那是因为数组是引用类型。

使用

ByVal
将引用类型传递给方法意味着引用是按值传递的 - 因此,如果您将引用更改为指向数组的另一个实例,则更改不会反映在您的方法之外。

如果您正在传递引用

ByRef
,并将其分配给方法内的另一个实例,那也会反映在外部。

有关更多详细信息,请阅读 Jon Skeet 的C# 中的参数传递(别担心,这也适用于 Vb.Net)。

© www.soinside.com 2019 - 2024. All rights reserved.