"Jon Skeet [C# MVP]" <sk***@pobox.com>
???????:11*********************@j4g2000prf.googleg roups.com...
On Jun 12, 11:26 am, "Jack" <j...@knight.comwrote:
>If I don't specify "ref" in the argument list when passing an array to
the
callee, I am passing the array (reference) by value.
Yes.
>But this makes me
confused because it actually means a "reference" of a "reference"??As I
pass
the array by value, the callee can change that array.
The callee can change the *contents* of the array.
>However,when I use ref, the callee and caller point to two different
arrays.
No they don't. Do you have some sample code which makes you think they
do?
The book is called "C# for experienced programmers"
On page 127-130,
Using system;
....
public class ArrayReferenceTest : System.Windows.Forms.Form
{ ...
private void showOutputButton_Click(
object sender, System.EventArgs e)
{
int[] firstArray = { 1, 2, 3 };
int[] firstArrayCopy = firstArray;
outputLabel.Text = "Test passing firstArray reference by value";
outputLabel.Text = "\n\n Contents of firstArray " + "before calling
FirstDouble:\n\t";
for (int i = 0; i < firstArray.Length; i++)
outputLabel.Text += firstArray[i] + " ";
FirstDouble (firstArray);
outputLabel.Text += "\n\nContents of firstArray after " + "calling
FirstDouble\n\t";
for (int i = 0; i < firstArray.Length; i++)
outputLabel.Text += firstArray[i] + " ";
if ( firstArray == firstArrayCopy )
outputLabel.Text += "\n\nThe references refer to the same
array\n";
else
outputLabel.Text += "\n\nThe references refer to different
arrays\n";
int [] secondArray = { 1, 2, 3 };
int [] secondArrayCopy = secondArray;
outputLabel.Text += "\nTest passing secondArray " +
"reference by reference";
outputLabel.Text += "\nContents of secondArray " +
"before calling SecondDouble:\n\t";
for (int i = 0; i < secondArray.Length; i++ )
outputLabel.Text += secondArray[i] + "\n";
SecondDouble (ref secondArray);
outputLabel.Text += "\n\nContents of secondArray " +
"after calling SecondDouble:\n\t";
for (int i = 0; i < secondArray.Length; i++)
outputLabel.Text += secondArray[i] + " ";
if (secondArray == secondArrayCopy)
outputLabel.Text +=
"\n\nThe references refer to the same array\n";
else
outputLabel.Text +=
"\n\nThe references refer to different arrays\n";
}
void firstDouble (int [] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;
array = new int[] { 11, 12, 13 };
}
void SeondDouble (ref int[] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;
array = new int[] { 11, 12, 13 };
}
}
=================================
results:
Test passing firstArray reference by value
Contents of firstArray before calling FirstDouble
1 2 3
Contents of firstArray after calling FirstDouble
2 4 6
The references refer to the same array
Test passing secondArray reference by reference
Contents of secondArray before calling
SecondDouble:
1 2 3
Contents of secondArray after calling SecondDouble:
11 12 13
The references refer to different arrays
============================
Thanks
Jack
>
>So could anyone please explain the 2 different situations?
If you use ref, the callee can change which array the parameter refers
to, and that change will be made to the caller's variable too.
See http://pobox.com/~skeet/csharp/parameters.html for more
information.
Jon