Concepts Of Arrays In C#

What are arrays?

  • Arrays are first class objects.
  • Array's actual values are stored on heap and the references are stored on stack.
  • Declare and allocate memory.
    1. Int[] ranks;  
    2. Ranks=new int[5];  
    Or
    1. Int[] ranks=new int[] {1,2,3,4,15};  
    2. Int[] ranks={1,3,5,6,8};  
    ranks2[i]=ranks[i];

    foreach (int I in ranks)
    Console.writeLine(I);


    Array is a set of similar elements stored in contiguous memory locations. It is a reference type in C#. An array can be created without explicitly using new keyword. The base index of an array is always zero. It is possible to declare an array and assign any array of the integer objects to it, regardless of the array’s length.
    1. Int [] ranks; // declare numbers as an int array of any size  
    2. Ranks = new int[5]; // it is a 5 element array  
    System.Array class is the base class array. It is an abstract class. All the elements are stored in an array are of same type. This class provides for creating, manipulating, sorting and searching the elements in an array. System.Array class implements ICloneable, IList, ICollection and IEnumerable interfaces.

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

  • Public static void Copy(Array sourceArray, Array destinationArray, int length)

    This function is used to copy the given number of elements from an array starting with first element and paste to another array starting at the first element. The length is a 32-bit integer. For example
    1. Copy(arry1, arry2,5);  

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

  • Public static void Reverse(Array array)

    This function is used to reverse the sequence of the elements in the entire one-dimensional array.
    1. Reverse(arry1);  
  • Public void CopyTo(Array array, int index)

    This function is used to copy all the elements of the one-dimensional array to another one-dimensional array starting at the specified destination array index. The index is 32-bit integer.
    1. CopyTo(arry1, arry2, 5);  
  • Public int GetLength(int dimension)

    This function is used to get the number of elements in the specified dimension of the array.
    1. GeLength(2);  

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};  

 

  • Declaration and initialization can be done in the same statement.
  • Ranks[1] will print 62.
  • A value can be assigned to an element in the array
    1. ranks[1] =50;  
  • Arrays can be printed, using a loop.
    1. for(int i=0; i<5; i++)  
    2. Console.WriteLine(“The {0} element in an array is {1}”, i+1, ranks[i]);  

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

 

  • Rectangular array

    It is an array, which can have more than one dimension.
    For example, the following creates a two-dimensional array with five rows and two columns, which is given below.
    1. Int [, ] numbers = new int [5,2] {{5,6},{8,9},{1,2},{5,8},{7,5}};  
    Two-dimensional array elements can be printed, using two loops, as shown below.
    1. for (int i=0; I < 5; i++)  
    2. for (int j=0; j < 2; j++)  
    3. Console.WriteLine(“Element({0},{1})={2}”,I , j , numbers[i, j]) ;  
  • Jagged Array

    It is an array-of -arrays. In this element are the arrays. They can be of different sizes and different dimensions. For example
    1. Int [ ] [] jaggedArray = new int[3] [];  
    To use Jagged array, the elements must be initialized, as shown below.
    1. jaggedArray[0] = new int[6];  
    2. jaggedArray[1] = new int[5];  
    Here, all the elements are a single dimensional array of the integers. The first array contains 6 integer elements; the second array contains 5 integer elements.

    It is also possible to fill the array elements with the values. In this case, there is no need of the array size,

    For example
    1. jaggedArray[0] = new int[] {5,6,2,7,6,8};  
    2. jaggedArray[1] = new int[5] {0,8,2,4};  
Ebook Download
View all
Learn
View all