What are arrays?

Some of the methods of an array class are given below.

The example given above copies the first 5 of arry1 and paste to arry2

Single dimensional arrays

In the declaration,

  1. Int [] ranks = new int[5];

5 is the upper limit of the array ranks i.e. the total number of elements that can be stored in it is 5.

  1. Int[] ranks = {52,62,8,26,88};

Passing Arrays to methods

When an array is passed as a parameter to a method, actual swapping of the values takes place. The actual elements are stored on Heap. Each element whether a value type or a reference type (like class Date object) is stored on the managed Heap.

  1. Public static void Swap(Date[] Arr) {
  2. Date temp;
  3. temp = Arr[0];
  4. Arr[0] = Arr[1];
  5. Arr[1] = temp;
  6. }
  7. //Code in Main() function
  8. Date[] dt = new Date[2];
  9. dt[0] = new Date(1, 2, 3);
  10. dt[1] = new Date(4, 5, 6);
  11. Console.WriteLine(“Before Swap” + dt[0] + ”\t” + dt[1]);
  12. Swap(dt);
  13. Console.WriteLine(“After Swap” + dt[0] + ”\t” + dt[1]);

Multidimensional arrays