ByRef 与 ByVal 的澄清 [英] ByRef vs ByVal Clarification

查看:29
本文介绍了ByRef 与 ByVal 的澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习处理客户端与 TCP 服务器的连接的课程.这是我迄今为止编写的代码:

I'm just starting on a class to handle client connections to a TCP server. Here is the code I've written thus far:

Imports System.Net.Sockets
Imports System.Net

Public Class Client
    Private _Socket As Socket

    Public Property Socket As Socket
        Get
            Return _Socket
        End Get
        Set(ByVal value As Socket)
            _Socket = value
        End Set
    End Property

    Public Enum State
        RequestHeader ''#Waiting for, or in the process of receiving, the request header
        ResponseHeader ''#Sending the response header
        Stream ''#Setup is complete, sending regular stream
    End Enum

    Public Sub New()

    End Sub

    Public Sub New(ByRef Socket As Socket)
        Me._Socket = Socket

    End Sub
End Class

因此,在我的重载构造函数中,我接受对 System.Net.Sockets.Socket实例引用,是的?

So, on my overloaded constructor, I am accepting a reference to an instance of a System.Net.Sockets.Socket, yes?

现在,在我的 Socket 属性上,设置值时,它需要是 ByVal.据我了解,内存中的实例复制,并且这个新实例被传递给value,并且我的代码将 _Socket 设置为在内存中引用此实例.是吗?

Now, on my Socket property, when setting the value, it is required to be ByVal. It is my understanding that the instance in memory is copied, and this new instance is passed to value, and my code sets _Socket to reference this instance in memory. Yes?

如果这是真的,那么我不明白为什么我要为除本机类型以外的任何东西使用属性.我想如果复制具有大量成员的类实例,性能可能会受到很大影响.此外,特别是对于这段代码,我认为复制的套接字实例不会真正起作用,但我还没有对其进行测试.

If this is true, then I can't see why I would want to use properties for anything but native types. I'd imagine there can be quite a performance hit if copying class instances with lots of members. Also, for this code in particular, I'd imagine a copied socket instance wouldn't really work, but I haven't tested it yet.

无论如何,如果你能证实我的理解,或者解释我模糊逻辑的缺陷,我将不胜感激.

Anyway, if you could either confirm my understanding, or explain the flaws in my foggy logic, I would greatly appreciate it.

推荐答案

请记住,ByVal 仍然会传递引用. 不同之处在于您获得了引用的副本.

Remember that ByVal still passes references. The difference is that you get a copy of the reference.

所以,在我的重载构造函数中,我接受对 System.Net.Sockets.Socket 实例的引用,是吗?

So, on my overloaded constructor, I am accepting a reference to an instance of a System.Net.Sockets.Socket, yes?

是的,但如果您要求 ByVal 代替,情况也是如此.不同之处在于,使用 ByVal 可以获得参考的副本 —你有新的变量.使用 ByRef,它是相同的变量.

Yes, but the same would be true if you asked for it ByVal instead. The difference is that with ByVal you get a copy of the reference — you have new variable. With ByRef, it's the same variable.

我的理解是复制内存中的实例

It is my understanding that the instance in memory is copied

没有.仅复制引用.因此,您仍在使用相同的实例.

Nope. Only the reference is copied. Therefore, you're still working with the same instance.

这是一个更清楚地解释它的代码示例:

Here's a code example that explains it more clearly:

Public Class Foo
   Public Property Bar As String
   Public Sub New(ByVal Bar As String)
       Me.Bar = Bar
   End Sub
End Class

Public Sub RefTest(ByRef Baz As Foo)
     Baz.Bar = "Foo"
     Baz = new Foo("replaced")
End Sub

Public Sub ValTest(ByVal Baz As Foo)
    Baz.Bar = "Foo"
    Baz = new Foo("replaced")
End Sub

Dim MyFoo As New Foo("-")
RefTest(MyFoo)
Console.WriteLine(MyFoo.Bar) ''# outputs replaced

ValTest(MyFoo)
Console.WriteLine(MyFoo.Bar) ''# outputs Foo

这篇关于ByRef 与 ByVal 的澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆