strange? by ref instead of by value

  • Thread starter Thread starter Andi Wolf
  • Start date Start date
A

Andi Wolf

Hello,

as i belived c# will always transfer the variables as by vale if not marked
with a ref infront of the decleration.
By using the folowing code there seams to be (at least for me) a strange
behavior:

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{

yValue = test(yValue);
return (yValue);
}

private static double[] test(double[] data)
{
data[1] = 11;
return (new double[] { 3, 4, 5 });
}
}
}


Main Programm:

using xyz;
....
double[] d = new double[] { 1, 0, 3, 0};
double[] d1 = new double[] { 1, 2, 3, 4};
d1 = Class1.Real(d, false);
....

If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't have
been changed at all?

Any ideas why this happens? Im using VS2005 SP1 and I think there are no
parameters in VS which can be changed to get this reaktion of the code?

regards Andi
 
Andi,

When you pass yValue to test, the array reference is passed by value.
In test, you could not assign a new array to the data parameter, but you
could most definitely modify items in whatever the reference points to. By
value and by reference only refer to the parameter, not to whatever the
parameter might point to if it is a reference.
 
Andi Wolf said:
as i belived c# will always transfer the variables as by vale if not marked
with a ref infront of the decleration.

Yes, it will. But for reference types, the value of the variable is a
reference, not the object itself. Arrays are reference types.

If I watch the variables d and d1 by the debugger i get the folowing
results: d1 := { 3, 4, 5 } and d := { 1, 11, 3, 0}. I would not have
expexted, that the d[1] value would have been changed to 11, because the d
parameter wasn' t given by the ref. In my opinion the d value shouln't have
been changed at all?

The value of the d variable hasn't changed. That variable's value is
just a reference to an array. The *contents* of the array has
changed, but the reference is the same thing.

See http://pobox.com/~skeet/csharp/parameters.html for more details on
this.
 
so the only way to avoid, that d is changed is to copy the array in to a
other array ?

namespace xyz
{
public class Class1
{
public static double[] Real(double[] yValue, bool invers)
{
double[] x = new double[yValue.Length];
yValue.CopyTo(x,0);
x = test(x);
return(x)
}
}
}

or is there a better way?

regards andi
 
Andi said:
so the only way to avoid, that d is changed is to copy the array in to a
other array ?

The only way if you need to pass an array to the method.

Another way could be to create a wrapper class for the array that
implements something like ICollection or IEnumerable, and that only
exposes the values as read-only.
 

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

Scrabble Value calculation for Welsh words 0
DLL import question 1
ref parameter 15
using ref keyword performance 6
ref parameter for any object 10
Strange behaviour 10
Move cells 1
b-tree 11

Back
Top