Assigning Array References


As with other objects, when you assign one array reference variable to another, you are simply changing the object to which the variable refers. You are not causing a copy of the array to be made, nor are you causing the contents of one array to be copied to the other. For example, consider this program:

 // Assigning array reference variables. using System; class AssignARef {   public static void Main() {     int i;     int[] nums1 = new int[10];     int[] nums2 = new int[10];     for(i=0; i < 10; i++) nums1[i] = i;     for(i=0; i < 10; i++) nums2[i] = -i;     Console.Write("Here is nums1: ");     for(i=0; i < 10; i++)       Console.Write(nums1[i] + " ");     Console.WriteLine();     Console.Write("Here is nums2: ");     for(i=0; i < 10; i++)       Console.Write(nums2[i] + " ");     Console.WriteLine();     nums2 = nums1; // now nums2 refers to nums1     Console.Write("Here is nums2 after assignment: ");     for(i=0; i < 10; i++)       Console.Write(nums2[i] + " ");     Console.WriteLine();    // now operate on nums1 array through nums2    nums2[3] = 99;     Console.Write("Here is nums1 after change through nums2: ");     for(i=0; i < 10; i++)       Console.Write(nums1[i] + " ");     Console.WriteLine();   } }

The output from the program is shown here:

 Here is nums1: 0 1 2 3 4 5 6 7 8 9 Here is nums2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 Here is nums2 after assignment: 0 1 2 3 4 5 6 7 8 9 Here is nums1 after change through nums2: 0 1 2 99 4 5 6 7 8 9

As the output shows, after the assignment of nums1 to nums2, both array reference variables refer to the same object.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net