Byref / Byval?

R

Rob Meade

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
 
J

Jon Paal

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....
 
K

Karl Seguin

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
 
J

Just Me

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.
 
D

dgk

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.
 
R

Rob Meade

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ByVal copy a variable 7
ByVal and ByRef 1
Byval vs. byref 5
cTor argument byVal or byRef 7
Byref 10
some logic for constructor 2
Custom membership provider - help needed 0
Disposing 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top