value type

  • Thread starter Thread starter RickN
  • Start date Start date
R

RickN

If you return a struct that holds an array of strings,
is the array of strings passed by value or by ref?

Thanks,
RickN
 
I'm fairly certain that strings use references. use the .Clone() method on
a string instance if you want to pass by value.

Chris
 
You never technically get an array of strings - you get an array of string
*references*. So regardless if you pass the struct by value (the 'default')
or by reference (ref, out) your strings' references will remain pointing at
the same strings (assuming no remoting). .NET strings are immutable, so
they somewhat appear to have value semantics in some way- but they in fast
are classes to which you get only a reference.
 
struct is passed by value. The array inside the struct is a pointer to the
first element in the array. Each element inside that array is a char* to
your string.
 
RickN said:
If you return a struct that holds an array of strings,
is the array of strings passed by value or by ref?

The struct will hold a reference to the array. The array itself will
consist of references to strings.

The array of string references itself isn't passed at all - the
reference to the array is returned with the rest of the struct data,
i.e. by value.

See http://www.pobox.com/~skeet/csharp/memory.html
and
http://www.pobox.com/~skeet/csharp/parameters.html

for some more information.
 
Back
Top