Arrays by ref (yes, yet another ?)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I see that array names are pointers and that a given array, say A[,], can be
altered within a sub when passed as an argument; whether it be by (ref
double[,] A) or by (double[,] A).

I do not clearly understand when to use (ref double[,] A) but I believe it
is when I want my sub to be able to change the identity of the array in
question (that is, change what A is pointing to). Is this correct?

More importantly for me is the question of which technique to use when I
know that I want the sub to operate on a specific array. Is there any
difference in data flow or efficiency between the two methods? Is there a
preferred SOP?
 
mr said:
I see that array names are pointers and that a given array, say A[,], can be
altered within a sub when passed as an argument; whether it be by (ref
double[,] A) or by (double[,] A).

I do not clearly understand when to use (ref double[,] A) but I believe it
is when I want my sub to be able to change the identity of the array in
question (that is, change what A is pointing to). Is this correct?

Yes.

You can always change the content of the array the reference points to.

By using ref you can get the reference to point to another array.
More importantly for me is the question of which technique to use when I
know that I want the sub to operate on a specific array. Is there any
difference in data flow or efficiency between the two methods? Is there a
preferred SOP?

I would not expect any measurable performance difference.

I would only use ref where it is really needed, because in
general it makes the code more difficult to read.

Arne
 
mr said:
I see that array names are pointers and that a given array, say A[,], can be
altered within a sub when passed as an argument; whether it be by (ref
double[,] A) or by (double[,] A).

I do not clearly understand when to use (ref double[,] A) but I believe it
is when I want my sub to be able to change the identity of the array in
question (that is, change what A is pointing to). Is this correct?

That's right.
More importantly for me is the question of which technique to use when I
know that I want the sub to operate on a specific array. Is there any
difference in data flow or efficiency between the two methods? Is there a
preferred SOP?

Just don't use the ref keyword unless you have to, or if you find that
it really makes things clearer.

When you send a reference type as an argument without the ref keyword, a
copy of the reference is sent. If you use the ref keyword a reference to
the reference is sent instead, which adds another level of indirection
whenever the object is used.

An example of where the ref keyword is used in the framework is the
Array.Resize method. The method actually doesn't resize the array (as
that is not possible in .NET), but creates a new array and copies the
data to it. The method could avoid the ref keyword by simply returning
the new array instead, but then the method would have to be renamed to
something like Array.CreateNewAndCopyTo to avoid misunderstandings.
 
Back
Top