Arrays in C#

Array

An array is a consecutive allocation of memory (of the same data type). Or an ordered arrangement in a specific type of thing.

Use of Arrays

Whenever we have more than one element of the same data type for the same task arrays can really make things easier. One prime benefit for using arrays is that all the locations are accessed by a single name (only the index varies), we can apply some intelligent looping over it.

Types of Arrays in C#

C# supports two types of arrays.

  • Rectangular Arrays.
  • Jagged Arrays.

Rectangular Arrays

In rectangular arrays, the number of elements in a dimension is the same for all. For example, if the number of columns for a row is 5 then all the rows will be have 5 columns.

Declaration and Initialization

Use the following syntax to declare a rectangular array.

For a single-dimension rectangular Array:

int [] Array1 = new int [5];

int [] Array2 = new int[] {10, 20, 30, 40, 50, } ;

int [] Array3 = new int [] { 10, 20, 30, 40, 50, 60 }; // Allowed

int [] Array4 = new int [5] {10, 20, 30, 40, 50,60 }; //Error 

In these statements the number of elements are 6 in the initialization; that exceeds the size, 5, so the compiler will raise an error.

We can also use a loop to initialize the array elements as in the following:

for(int i=0;i<Array1.Length;i++)
{

    Array1 [i] =99;

}

The following is an example of accessing the Array elements:

for(int i=0;i<Array1.Length;i++)

{

    System.Console.WriteLine(“Array Elements: ”+Array1[i]);

}     

2D Rectangular Array

Above I explained the rules and those rules also apply to 2D arrays just here include the rows and columns and here "I" will be the row and "j" will be the column.

Int[,] TwoDArray1 =new int[3,2];

Int[,] TwoDArray2 =new int[3,2]{{2,4},{5,6},{8,9}};

Int[,] TwoDArray3 =new int[3,2]{{2,2},{4,3},{9,9}};

for(int i=0;i<TwoDArray1.GetLength(0);i++)

{

    for(int j=0;j<TwoDArray1.GetLength(1);j++)

    {

        TwoDArray1[i][j]=65;

    }

}

The following is an example of accessing the Array elements:

for(int i=0;i<TwoDArray1.GetLength(0);i++)

{

    for(int j=0;j<TwoDArray1.GetLength(1);j++)

    {

         System.Console.WriteLine(“Array Elements: ”+TwoDArray1[i][j]);

    }

}

Here GetLength() is a built-in function to get the length of the array. And 0 will indicate the first dimension of the TwoDArray1 and 1 indicates the second dimension of the TwoDArray1.

What a Jagged Array is

In a jagged array, the number of elements in each dimension will be different. For example in a classroom there are 5 rows of benches but it is not necessary that in each bench the same number of students sit, there can be a varying number of students in each row (bench).

Declaration

We can declare a jagged array using the following syntax.

Int[][] JArray=new int[4][];

JArray[0]=new int[]{4,7,2};

JArray[1]=new int[]{4,2};

JArray[2]=new int[]{9};

JArray[3]=new int[]{5,3,2};

Initialization

for(int i=0;i<JArray.Length;i++)

{

    for(int j=0;j<JArray[j].Length;j++)

    {

        JArray[i][j]=Convert.ToInt32(System.Console.ReadLine());

    }

} 

How we can access the elements from a Jagged Array?

For this we will use a loop.

For(int i=0;i<JArray.Length;i++)

{

    For(int j=0;j<JArray[j].Length;j++)

    System.Console.WriteLine(“Output: ”+JArray[i][j]);

}