arrays are a reference type

Arrays are a collection of like data with a single name.
Elements of the array are accessed using the name and a unique array index.


. simple arrays
Array ===== - fixed # of components that have same data type - components are accessed using their positions [index] in the array CODE:
    
   // create an array
   // fill array with square of its index
   // output array
   using System;
   class ArrayDemo
   {
       public static void Main ()
       {
           int[] nums;        // creates REFERENCE to the array
           nums = new int[5]; // INSTANTIATE a new array object
                              // NOTE could do as: int[] nums = new int[5];
           int i;

           // loop and assign
           for (i=0; i<5; i++)
               nums[i] = i*i;

           // loop and output
           for (i=0; i<nums.Length; i=i+1)
               Console.WriteLine( i.ToString("#,##0").PadRight(5) +
                                  nums[i].ToString("#,###").PadLeft(10) );
       }
   }
       
   
OUTPUT: 0 1 1 2 4 3 9 4 16
       
       public static void Main ()
       {
           const int SIZE=100;
           int[] nums; // creates REFERENCE to the array
           int i;
           nums = new int[SIZE]; // INSTANTIATE

           // loop and assign
           for (i=0; i<SIZE; i=i+1)
               nums[i] = i*i;

           // loop and output (use Length property of array)
           for (i=0; i<nums.Length; i++)
               Console.WriteLine( i.ToString("#,##0").PadRight(5) +
                                  nums[i].ToString("#,###").PadLeft(10) );
       }
           
   
Output: 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 ... (removed) ... 95 9,025 96 9,216 97 9,409 98 9,604 99 9,801
Array index =========== - any expression that evaluates to non-negative integer - value of index is less than size of the array, n (0 to n-1) - base address of array is address of first array component Declaring an array =================
         // declares an array 'num' of 3 int components indexed 0-2
         int[] num = new int [3]; 

         // assign a value to each element
         num[0] = 99;
         num[1] = 27;
         num[2] = 10+3;
   
components: num[0], num[1], num[2] contents: 99 27 13 Accessing Array Components ========================== To access an array component: arrayName[ index ] where index is a nonnegative integer or expression index value specifies the position in the array [ ] operator is the array subscripting operator - index starts at 0 Declare and Initialize arrays: ==============================
   int[] nums1 = new int[5]; // all zeroed
   
   int[] nums2 = new int[5] {1,2,3,4,5};
   
   int[] nums3 = new int[] {1,2,3,4,5};
   
   string[] languages1 = {"C#", "COBOL", "Java", "C++", "Visual Basic", "Pascal"}; // no "new"

   string[] languages2;
   languages2 = new string[]{"C#", "COBOL", "Java", "C++", "Visual Basic", "Pascal"};
               
   string[] languages3 = new string[]{"C#", "COBOL", "Java","C++", "Visual Basic", "Pascal"};
                        
   string[] languages4 = new string[6]{"C#", "COBOL", "Java","C++", "Visual Basic", "Pascal"};

   string[] languages5 = new string[6]; // null
   
   

. code: filling, summing and printing arrays (foreach , random, %)
   // three arrays
   // fill nums array with value of its own index
   // fill evens array with random even numbers 
   // fill sums array with the sum of each element from the 2 arrays  
   using System;
   class Program
   {
      static void Main()
      {
          const int N = 1000;
          int number;
          int[] nums = new int[N];
          int[] evens = new int[N];
          long[] sums = new long[N];
            
          // put numbers in array
          for (int i=0; i< N; i++)
          {
              nums[i] = i;
          }
        
          // put even random numbers in array
          Random rnd = new Random();
          for (int i=0; i<N; i++)
          {
             do
             {
                number = rnd.Next();
             } while (number % 2 == 1);  // odd
             evens[i] = number;
          }
    
          // array has a Length property
          // add the elements of both arrays
          for (int i=0; i<nums.Length; i++)
          {
             sums[i] = nums[i] + evens[i];     
          }
    
          // another for: foreach
          foreach (long element in sums)
          {
            Console.WriteLine(element);
          }

          // hold console open
          Console.ReadKey();
      }
   }

   

. code: Rob Miles - readInt() & readString()
  
   //////////////////////////////////////
   // from rob miles yellow book....
   using System;
   class ArrayDemo
   {
      public static void Main()
      {
         const int SCORE_SIZE = 5;
         int[] scores = new int[SCORE_SIZE];

         // get scores from user
         for (int i = 0; i < SCORE_SIZE; i = i + 1)
            scores[i] = readInt("Score " + i + ": ", 0, 10000);

         // array has been filled, now print BACKWARDS
         for (int i = scores.Length - 1; i >= 0; i--)
            Console.WriteLine(scores[i].ToString("#,##0"));

         // hold console open
         Console.ReadKey();
      }

      // prompts and reads an int within a range
      static int readInt(string prompt, int low, int high)
      {
         int result;
         try
         {
            do
            {
               string intString = readString(prompt);
               result = int.Parse(intString); // TryParse here?
            } while ((result < low) || (result > high));
         }
         catch
         {
            // bad data
            return -9999; // indicate error - kludgey
         }
         return result;
      }

      // prompts and reads a string
      static string readString(string prompt)
      {
         string result;
         do
         {
            Console.Write(prompt);
            result = Console.ReadLine();
         } while (result == "");
         return result;
      }
   }
   

. code: strings as arrays
  
   // Listing 2.54: Reversing a String from michaelis, Essential C#
   class Palindrome
   {
      static void Main()
      {
         string reverse, palindrome;
         char[] temp;
         System.Console.Write("Enter a palindrome: ");
         palindrome = System.Console.ReadLine();
         
         // Remove spaces and convert to lowercase
         reverse = palindrome.Replace(" ", "");
         reverse = reverse.ToLower();
         
         // Convert to an array
         temp = reverse.ToCharArray();
   
         // Reverse the array
         System.Array.Reverse(temp);
         
         // Convert the array back to a string and
         // check if reverse string is the same.
         if( reverse == new string(temp) )
         {
            System.Console.WriteLine("\"{0}\" is a palindrome.", palindrome);
         }
         else
         {
            System.Console.WriteLine("\"{0}\" is NOT a palindrome.", palindrome);
         }
      }
   }
   
OUTPUT : Enter a palindrome: NeverOddOrEven "NeverOddOrEven" is a palindrome.