Arrays in C# 



 An array is a data structure in which one variable can hold many values, which are accessed through computed indices. The values contained in an array are called the elements of the array. The elements of the array type can be of any type, including the array type.

Arrays in C# are reference types and are very much objects in their own right. The following code shows how to declare references to an int array:

int [ ] a ; // declare int array reference
int [ ] a1, a2 ; // declare two array references

Here the type is int[ ] (i.e ‘reference to int array’). It is important to note here that we are not declaring the array itself but a reference to it. This merely sets aside space for reference to the array. Once we have declared the array reference, we can construct the array as follows:

a = new int [ 25 ] ; // create a 25 element int array

C# also uses a zero based scheme to access array elements like C, this means that the first element in the array is considered to be at 0th position. Initializing the array elements can be done as shown

int [ ] odd = new int [ ] {3, 5, 7, 9 } ;
 // which can be shortened to
int [ ] odd = { 3, 5, 7, 9 } ;

The compiler works out the size for itself and we need not give it explicitly.


Multidimensional Arrays 

 

Multidimensional arrays are of two types – rectangular and jagged. C# has different syntax for rectangular and jagged arrays

 

Rectangular Arrays:

In rectangular arrays every row is of same length. Following code snippet will give you an idea about rectangular arrays

 

using System ;
class Rectarray
{

static void Main ( )
{

// declare 1 dimensional rectangular array of type int 
int [ ] odd1 ; 

//define and hence create a 4 element int array
odd1 = new int [ ] { 3, 5, 7, 9 } ;


 // Declare and define an array of int with 4 elements 
int [ ] even1 = { 2, 4, 6, 8 } ;


// declare 2D array
int [ , ] odd2 ; 

// define a 2D array of size 2X4
odd2 = new int [ , ] { { 3, 5, 7, 9 }, { 11, 13, 15, 17 } } ;

 

// declare and define
int [ , ] even2 = { {2, 4, 6, 8 }, {10, 12, 14, 16 } } ;

// declare 3D array
int [ , , ] odd3; 

 // define a 3D array
odd3 = new int [ , , ] 

{

{  

{ 3, 5, 7, 9 },  { 11, 13, 15, 17 }

},
{

{ 21, 23, 25, 27 } , { 31, 33, 35, 37 }

}

} ;

 // define and declare

int [ , , ] even3= 
{

{

{ 2, 4, 6, 8 } , { 10, 12, 14, 16 }

},
{

{ 20, 22, 24 ,26 }, { 30, 32, 34, 36 }

}

} ;

// write out some elements
Console.WriteLine ( “{0}”, odd1 [ 1 ] ) ;
Console.WriteLine ( “{0}”, even2 [ 1, 1 ] ) ;
Console.WriteLine ( “{0}”, odd3 [ 1, 1, 1 ] ) ;

}

 

}

The output here is:
5
12
33

 

So we can see that the syntax for multidimensional arrays is a simple extension to the single dimensional case using nested curly brackets.

 

Jagged Arrays:

 

The syntax for declaring jagged arrays is different. A jagged array is an array of 1D arrays (each of which can be of different length), and this means we have to declare the jagged array itself plus each of the 1D arrays, which makes the array up.

You may think of initializing the jagged array on the same lines of a rectangular array somewhat as shown below

 

static void Main ( string [ ] args)
{

int [ ] [ ] odd ;
odd = new int [ 2 ] [ ] { { 3, 5, 7 }, { 11, 13 } } ;
for ( int i = 0 ; i <= 2; i++ )
{

Console.WriteLine ( "{0}", odd [ 0 ] [ i ] ) ;

}
for ( int j = 0; j <= 1 ; j++ )
{

Console.WriteLine ( "{0}", odd [ 1 ] [ j ] ) ;

}

}


BUT! The Compiler flashes an error: 

 

This is because, the compiler calculates the size by looking at the no of elements in each row, but as it is not same in jagged array it flashes an error. A better and correct way to do this is as shown:

 

static void Main ( string [ ] args )
{

int [ ] [ ] odd ; // array of ( arrays )
odd = new int [ 2 ][ ] ;
odd[0] = new int[3] { 3 , 5, 7 } ;
odd[1] = new int[2] {11,13};
for ( int i = 0 ; i <= 2 ; i++ )
{

Console.WriteLine ( "{0}", odd [ 0 ] [ i ] ) ;

}
for ( int j = 0 ; j <= 1 ; j++ )
{

Console.WriteLine ( "{0}", odd [ 1 ] [ j ] ) ;

}

}


Here we get the output as:
3
5
7
11
13


For 3D arrays we can proceed as follows:

 

even = new int [ 2 ] [ ] [ ] ; // create a 3D array
even[0] = new int [ 3 ] [ ] ;
even[0][0] = new int [ 3 ] ;
even[0][1] = new int [ 4 ] ;
/*And so on ,we can also initialize the elements individually though it’s 
the most primitive method*/

 

The ways we access the two types of arrays also differ. With rectangular array all indices are within one set of square brackets, while for a jagged arrays each element is within its own square brackets.