Arrays in C#

 

An array is a collection of same type variables which can be accessed using numeric index. You can store multiple variables of the same type in an array data structure.

 

There are three types of arrays

 

1.         Single Dimensional Arrays

2.         Multi Dimensional Arrays

3.         Jagged Arrays

 

Single Dimensional Arrays:-

It is the simplest form of array, it's kind of a row with n columns where each column is accessed with the zero based index

 

Array of ten integers will look like this: int[] array = newint[10];

 

This array contains the elements from array[0] to array[9]. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero

 

An array that stores string elements can be declared in the same way. For example:

string[] stringArray = newstring[6];

 

Example for Single Dimensional Arrays.

 

Public class SingleDimentionalArraysClass

{

                Public staticvoid Main()

                {

int[] array1 = newint[5];

 

                int[] array2 = newint[] { 1, 3, 5, 7, 9 };

 

                int[] array3 = { 1, 2, 3, 4, 5, 6 };

    }

}

 


Multi Dimensional Array :-

Arrays can have more than one dimension.The most widely used are two dimensional arrays.

 

Public class MultiDimentionalArraysClass

{

        Public staticvoid Main()

       {

             int[,] array2D = newint[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

             // The same array with dimensions specified.

            int[,] array2Da = newint[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

            // A similar array with string elements.

             string[,] array2Db = newstring[3, 2] { { "one", "two" }, { "three", "four" },

                                        { "five", "six" } };

             // Three-dimensional array.

             int[, ,] array3D = newint[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },

                                 { { 7, 8, 9 }, { 10, 11, 12 } } };

              // The same array with dimensions specified.

              int[, ,] array3Da = newint[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },

                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

           }

}

 

 

How to assign the values to matrix[10,10] array

 

for (int i = 0; i < 10; i++)

{

    for (int j = 0; j < 10; j++)

    {

        matrix[i, j] = i * 10 + j + 1;                   

    }                

}

In case of multidimensional arrays, knowing number of dimensions is sometimes necessary to have more grip over the array. Rank property returns number of dimensions of the array  and it can be retrieved using matrix.Rank;

 

 

Jagged Arrays:-

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

 

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

int[][] jaggedArray = newint[3][];

 

Before you can use jaggedArray, its elements must be initialized. You can initialize the elements like this:

 

jaggedArray[0] = newint[5];

jaggedArray[1] = newint[4];

jaggedArray[2] = newint[2];

 

 

Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.

You can also initialize the array upon declaration like this:

 

 

  int[][] jaggedArray2 = newint[][]

{

    newint[] {1,3,5,7,9},

    newint[] {0,2,4,6},

    newint[] {11,22}

};


You can use the following shorthand form. Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements:

 

    int[][] jaggedArray3 =

{

    newint[] {1,3,5,7,9},

    newint[] {0,2,4,6},

    newint[] {11,22}

};