Byref / Byval?

Discussion in 'ASP .Net' started by Rob Meade, Dec 28, 2006.

  1. Rob Meade

    Rob Meade Guest

    Hi all,

    Until just recently most of my functions would have all been using Byval,
    when I realised the advantages of ByRef, where appropriate I have used it
    instead, my question - are there any performance reasons for using ByRef
    rather than ByVal?

    In my (tiny) mind, I see ByRef as passing a reference to an already created
    object, I can then use values of that object or do things to it, where-as
    ByVal creates a separate instance of it (am I correct?) - if this is the
    case, presumably I now have 2 instances of this object which need to be
    destroyed - so presumably, not having to destroy two things is better?

    Any info appreciated..

    Regards

    Rob
     
    Rob Meade, Dec 28, 2006
    #1
    1. Advertising

  2. Rob Meade

    Jon Paal Guest

    I don't think performance is a criteria for usage, the criteria is purpose. if you want to modify the value of the object or simply
    use the value of the object....


    "Rob Meade" <> wrote in message news:%...
    > Hi all,
    >
    > Until just recently most of my functions would have all been using Byval, when I realised the advantages of ByRef, where
    > appropriate I have used it instead, my question - are there any performance reasons for using ByRef rather than ByVal?
    >
    > In my (tiny) mind, I see ByRef as passing a reference to an already created object, I can then use values of that object or do
    > things to it, where-as ByVal creates a separate instance of it (am I correct?) - if this is the case, presumably I now have 2
    > instances of this object which need to be destroyed - so presumably, not having to destroy two things is better?
    >
    > Any info appreciated..
    >
    > Regards
    >
    > Rob
    >
     
    Jon Paal, Dec 28, 2006
    #2
    1. Advertising

  3. Rob Meade

    Karl Seguin Guest

    There are two types of values in .NET : Value types and Reference types.

    Value types are all the core primitives, including ints, chars, bytes and
    any custom structure you create.

    Reference types are everything else, including string, exceptions, web/win
    controls, ..., ... , ... or any custom objects you use

    For value types, ByRef and ByVal probably have the impact you think it
    should - if you pass it byref and change the value, it won't change in the
    calling method..

    For reference types the difference is extremely subtle. If you understand
    that any variable that holds a reference type is actually a pointer
    then...ByVal passes a copy of the pointer, so if you change any properties
    or call a method on the object, it WILL actually modify the object (which
    probably ISN'T what you would expect, since it's byval). ByRef passes the
    actual pointer, so not only does it behave exactly like ByVal, but if you
    chance the point location (via assignment), it too will change the object...

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Rob Meade" <> wrote in message
    news:%...
    > Hi all,
    >
    > Until just recently most of my functions would have all been using Byval,
    > when I realised the advantages of ByRef, where appropriate I have used it
    > instead, my question - are there any performance reasons for using ByRef
    > rather than ByVal?
    >
    > In my (tiny) mind, I see ByRef as passing a reference to an already
    > created object, I can then use values of that object or do things to it,
    > where-as ByVal creates a separate instance of it (am I correct?) - if this
    > is the case, presumably I now have 2 instances of this object which need
    > to be destroyed - so presumably, not having to destroy two things is
    > better?
    >
    > Any info appreciated..
    >
    > Regards
    >
    > Rob
    >
     
    Karl Seguin, Dec 28, 2006
    #3
  4. Rob Meade

    Just Me Guest

    AND . . .

    ByVal on an object will place the object on the stack rather than on the
    heap, this has some performance implications, but its not likely you'll
    notice it.


    "Karl Seguin" <> wrote in
    message news:...
    > There are two types of values in .NET : Value types and Reference types.
    >
    > Value types are all the core primitives, including ints, chars, bytes and
    > any custom structure you create.
    >
    > Reference types are everything else, including string, exceptions, web/win
    > controls, ..., ... , ... or any custom objects you use
    >
    > For value types, ByRef and ByVal probably have the impact you think it
    > should - if you pass it byref and change the value, it won't change in the
    > calling method..
    >
    > For reference types the difference is extremely subtle. If you understand
    > that any variable that holds a reference type is actually a pointer
    > then...ByVal passes a copy of the pointer, so if you change any properties
    > or call a method on the object, it WILL actually modify the object (which
    > probably ISN'T what you would expect, since it's byval). ByRef passes the
    > actual pointer, so not only does it behave exactly like ByVal, but if you
    > chance the point location (via assignment), it too will change the
    > object...
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.codebetter.com/
    >
    >
    > "Rob Meade" <> wrote in message
    > news:%...
    >> Hi all,
    >>
    >> Until just recently most of my functions would have all been using Byval,
    >> when I realised the advantages of ByRef, where appropriate I have used it
    >> instead, my question - are there any performance reasons for using ByRef
    >> rather than ByVal?
    >>
    >> In my (tiny) mind, I see ByRef as passing a reference to an already
    >> created object, I can then use values of that object or do things to it,
    >> where-as ByVal creates a separate instance of it (am I correct?) - if
    >> this is the case, presumably I now have 2 instances of this object which
    >> need to be destroyed - so presumably, not having to destroy two things is
    >> better?
    >>
    >> Any info appreciated..
    >>
    >> Regards
    >>
    >> Rob
    >>

    >
     
    Just Me, Dec 28, 2006
    #4
  5. Rob Meade

    dgk Guest

    On Thu, 28 Dec 2006 14:55:57 -0500, "Karl Seguin"
    <> wrote:

    >There are two types of values in .NET : Value types and Reference types.
    >
    >Value types are all the core primitives, including ints, chars, bytes and
    >any custom structure you create.
    >
    >Reference types are everything else, including string, exceptions, web/win
    >controls, ..., ... , ... or any custom objects you use
    >
    >For value types, ByRef and ByVal probably have the impact you think it
    >should - if you pass it byref and change the value, it won't change in the
    >calling method..
    >
    >For reference types the difference is extremely subtle. If you understand
    >that any variable that holds a reference type is actually a pointer
    >then...ByVal passes a copy of the pointer, so if you change any properties
    >or call a method on the object, it WILL actually modify the object (which
    >probably ISN'T what you would expect, since it's byval). ByRef passes the
    >actual pointer, so not only does it behave exactly like ByVal, but if you
    >chance the point location (via assignment), it too will change the object...
    >
    >Karl


    The performance implication of what Karl just said is that you aren't
    creating another instance of an object when you pass it by reference.
    You're only creating another pointer to it. So the performance hit is
    much less than you might think. If you change a property of an object
    that was passed by ref, perhaps color, to RED, then that property is
    RED when the caller returns the object.

    The idea of actually creating another instance of the object is
    problematic. If the object contains other objects, does your
    duplicated object contain the original subobjects or does the copy
    clone those also. I think that's the difference between a shallow copy
    and a deep copy.
     
    dgk, Dec 28, 2006
    #5
  6. Rob Meade

    Rob Meade Guest

    Thanks all :eek:)

    wow! There's some reading!

    hehe...thanks for all of the replies, you've enlightened me and given me
    some more things to investigate..

    Cheers

    Rob
     
    Rob Meade, Dec 28, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Waz...
    Replies:
    0
    Views:
    421
    David Waz...
    Jul 4, 2003
  2. Goncalo

    ByVal and ByRef

    Goncalo, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    6,096
    Dmitriy Lapshin [C# / .NET MVP]
    Dec 4, 2003
  3. Iams

    Byval vs. byref

    Iams, Feb 24, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,814
  4. xamman
    Replies:
    1
    Views:
    347
    Marina Levit [MVP]
    Dec 22, 2006
  5. ManicQin

    cTor argument byVal or byRef

    ManicQin, Aug 2, 2008, in forum: C++
    Replies:
    7
    Views:
    361
    James Kanze
    Aug 2, 2008
Loading...

Share This Page