By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

Byref / Byval?

P: n/a
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
Dec 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <ku***************@edaem.borwrote in message news:%2***************@TK2MSFTNGP06.phx.gbl...
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

Dec 28 '06 #2

P: n/a
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" <ku***************@edaem.borwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
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
Dec 28 '06 #3

P: n/a
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" <ka********@removeopenmymindremovemetoo.andmenetwr ote in
message news:eJ**************@TK2MSFTNGP04.phx.gbl...
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" <ku***************@edaem.borwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>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

Dec 28 '06 #4

P: n/a
dgk
On Thu, 28 Dec 2006 14:55:57 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:
>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.
Dec 28 '06 #5

P: n/a
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
Dec 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.