Passing a Reference Type as Value for Array
using System; class Program/*from w w w.ja v a 2 s .c om*/ { static void CheckMe(int[] arr) { arr[0] = 15; arr[1] = 25; arr = new int[3] { 100, 200, 300 }; Console.WriteLine("Inside CheckMe(),arr[0]={0}", arr[0]);//100 Console.WriteLine("Inside CheckMe(),arr[1]={0}", arr[1]);//200 Console.WriteLine("Inside CheckMe(),arr[2]={0}", arr[2]);//300 } static void Main(string[] args) { int[] myArray = { 1, 2, 3 }; Console.WriteLine("At the beginning,myArray[0]={0}", myArray[0]);//1 Console.WriteLine("At the beginning,myArray[1]={0}", myArray[1]);//2 Console.WriteLine("At the beginning,myArray[2]={0}", myArray[2]);//3 CheckMe(myArray); Console.WriteLine("At the end,myArray[0]={0}", myArray[0]);//15 Console.WriteLine("At the end,myArray[1]={0}", myArray[1]);//25 Console.WriteLine("At the end,myArray[2]={0}", myArray[2]);//3 } }
In the CheckMe() method, once we created a new array, the reference array started pointing to a new array.
no change is imposed in the original array, which was created inside Main().
After that operation, we were dealing with two different arrays.