Best practice ByVal or ByRef

  • Thread starter Thread starter rob
  • Start date Start date
R

rob

I have have a xmlTextWriter that calls a number of sub
routines (up to 60), the only paramter past in is the
xmlTextWriter and the paramenter past out is the
xmlTextWriter
Should I pass this paramter byVal or byRef
I know passing byVal is the best praticse but as the
xmlTextWriter gets larger, in my mind it seems like an
inefficient use of resources to create some many copy's of
the same variable
What is the best practise in a case like this
 
rob said:
I have have a xmlTextWriter that calls a number of sub
routines (up to 60), the only paramter past in is the
xmlTextWriter and the paramenter past out is the
xmlTextWriter
Should I pass this paramter byVal or byRef
I know passing byVal is the best praticse but as the
xmlTextWriter gets larger, in my mind it seems like an
inefficient use of resources to create some many copy's of
the same variable
What is the best practise in a case like this

As an xmlTextWriter is probably a reference type, the size of the object
doesn't matter. It's always only the reference (4 bytes) passed.
 
Hello,

rob said:
I have have a xmlTextWriter that calls a number of sub
routines (up to 60), the only paramter past in is the
xmlTextWriter and the paramenter past out is the
xmlTextWriter
Should I pass this paramter byVal or byRef
I know passing byVal is the best praticse but as the
xmlTextWriter gets larger, in my mind it seems like an
inefficient use of resources to create some many copy's of
the same variable

Have a look at the docs for the difference between ByVal and ByRef for
reference types. Passing the XmlTextWriter ByVal will not create a copy of
the XmlTextWriter.

Regards,
Herfried K. Wagner
 
rob said:
I have have a xmlTextWriter that calls a number of sub
routines (up to 60), the only paramter past in is the
xmlTextWriter and the paramenter past out is the
xmlTextWriter
Should I pass this paramter byVal or byRef
I know passing byVal is the best praticse but as the
xmlTextWriter gets larger, in my mind it seems like an
inefficient use of resources to create some many copy's of
the same variable
What is the best practise in a case like this

ByVal.

XmlTextWriter is a reference type. You can tell this because it does not
inherit System.ValueType. For reference types pass them ByVal 99.999999% of
the time.

When you pass a reference type ByVal the only thing that actually gets
copied is the 32 bit heap address of the referenced object. Only pass
reference types ByRef if you need to change which object is referenced.

When you pass a reference type ByRef what gets copied is the stack address
of the pointer that holds the heap address of the refrenced object. The
code in the invoked procedure can then edit the value of this pointer making
it point to a different heap address. This can be highly confusing and
unexpected, so in addition to being no more efficient, passing reference
types ByRef is bad practice unless you intend to use this behavior.



David
 
Back
Top