These are example from a c# book that I am reading just having a little trouble grasping what this example is actually doing would like an explanation to help me further understand what is happening here.
//creates and initialzes firstArray
int[] firstArray = { 1, 2, 3 };
//Copy the reference in variable firstArray and assign it to firstarraycopy
int[] firstArrayCopy = firstArray;
Console.WriteLine("Test passing firstArray reference by value");
Console.Write("\nContents of firstArray " +
"Before calling FirstDouble:\n\t");
//display contents of firstArray with forloop using counter
for (int i = 0; i < firstArray.Length; i++)
Console.Write("{0} ", firstArray[i]);
//pass variable firstArray by value to FirstDouble
FirstDouble(firstArray);
Console.Write("\n\nContents of firstArray after " +
"calling FirstDouble\n\t");
//display contents of firstArray
for (int i = 0; i < firstArray.Length; i++)
Console.Write("{0} ", firstArray[i]);
// test whether reference was changed by FirstDouble
if (firstArray == firstArrayCopy)
Console.WriteLine(
"\n\nThe references refer to the same array");
else
Console.WriteLine(
"\n\nThe references refer to different arrays");
//method firstdouble with a parameter array
public static void FirstDouble(int[] array)
{
//double each elements value
for (int i = 0; i < array.Length; i++)
array[i] *= 2;
//create new object and assign its reference to array
array = new int[] { 11, 12, 13 };
Basically there is the code what I would like to know is that the book is saying if the array is passed by value than the original caller does not get modified by the method(from what i understand). So towards the end of method FirstDouble they try and assign local variable array to a new set of elements which fails and the new values of the original caller when displayed are 2,4,6.
Now my confusion is how did the for loop in method FirstDouble modify the original caller firstArray to 2,4,6 if it was passed by value. I thought the value should remain 1,2,3.
Thanks in advance
The key to understanding this is to know the difference between a value type and a reference type.
For example, consider a typical value type,
int.After this code has executed,
ahas the value 2, andbhas the value1. Becauseintis a value type,b = atakes a copy of the value ofa.Now consider a class:
Because classes are reference types,
b = amerely assigns the reference rather than the value. Sobandaboth refer to the same object. Hence, aftera.MyProperty = 2executes,b.MyProperty == 2sinceaandbrefer to the same object.Considering the code in your question, an array is a reference type and so for this function:
the variable
arrayis actually a reference, becauseint[]is a reference type. Soarrayis a reference that is passed by value.Thus, modifications made to
arrayinside the function are actually applied to theint[]object to whicharrayrefers. And so those modifications are visible to all references that refer to that same object. And that includes the reference that the caller holds.Now, if we look at the implementation of this function:
there is one further complication. The
forloop simply doubles each element of theint[]that is passed to the function. That’s the modification that the caller sees. The second part is the assignment of a newint[]object to the local variablearray. This is not visible to the caller because all it does is to change the target of the referencearray. And since the referencearrayis passed by value, the caller does not see that new object.If the function had been declared like this:
then the reference
arraywould have been passed by reference and the caller would see the newly created object{ 11, 12, 13 }when the function returned.