Assigning array reference variables : Array : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array »

 

Assigning array reference variables









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Assigning array reference variables. 
 
using System; 
 
public 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(); 
  }  
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array