// ArrayReferenceTest.cs // Testing the effects of passing array references // by value and by reference. using System; public class ArrayReferenceTest { /// /// ************* MAIN *************** /// /// public static void Main(string[] args) { int[] array = { 1, 2, 3, 4, 5 }; Console.WriteLine("Effects of passing reference to entire array:\n" + "The values of the original array are:"); // output original array elements foreach (int value in array) { Console.Write(" {0}", value); } ModifyArray(array); // pass array reference Console.WriteLine("\n\nThe values of the modified array are:"); // output modified array elements foreach (int value in array) { Console.Write(" {0}", value); } Console.WriteLine("\n\nEffects of passing array element value:\n" + "array[3] before ModifyElement: {0}", array[3]); ModifyElement(array[3]); // attempt to modify array[3] Console.WriteLine("array[3] after ModifyElement: {0}", array[3]); // // PASS BY REFERENCE PART // // create and initialize firstArray int[] firstArray = { 1, 2, 3 }; // copy the reference in variable firstArray 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 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"); // create and initialize secondArray int[] secondArray = { 1, 2, 3 }; // copy the reference in variable secondArray int[] secondArrayCopy = secondArray; Console.WriteLine("\nTest passing secondArray " + "reference by reference"); Console.Write("\nContents of secondArray " + "before calling SecondDouble:\n\t"); // display contents of secondArray before method call for (int i = 0; i < secondArray.Length; i++) { Console.Write("{0} ", secondArray[i]); } // pass variable secondArray by reference to SecondDouble SecondDouble(ref secondArray); Console.Write("\n\nContents of secondArray " + "after calling SecondDouble:\n\t"); // display contents of secondArray after method call for (int i = 0; i < secondArray.Length; i++) { Console.Write("{0} ", secondArray[i]); } // test whether reference was changed by SecondDouble if (secondArray == secondArrayCopy) Console.WriteLine("\n\nThe references refer to the same array"); else Console.WriteLine("\n\nThe references refer to different arrays"); } /// /// multiply each element of an array by 2 /// /// public static void ModifyArray(int[] array2) { for (int counter = 0; counter < array2.Length; counter++) { array2[counter] *= 2; } } /// /// multiply argument by 2 /// /// public static void ModifyElement(int element) { element *= 2; Console.WriteLine("Value of element in ModifyElement: {0}", element); } /// /// modify elements of array and attempt to modify reference /// /// public static void FirstDouble(int[] array) { // double each element's 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 }; } /// /// modify elements of array and change reference array /// to refer to a new array /// /// public static void SecondDouble(ref int[] array) { // double each element's 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 }; } }