As described earlier, an array is used to store same type of data.
Classification of Arrays
Arrays in C# can be classified as below:
- Single-Dimensional Arrays
- Multidimensional Arrays
- Jagged Arrays
Arrays with single dimension has been discussed in the earlier chapters of this tutorial. In this chapter we will discuss Multidimensional and Jagged arrays.
C# Multidimensional Arrays
Arrays having more than one dimension are called as multidimensional.
A two-dimensional array is like a rectangular/square grid having fixed number of rows and columns.
You can create arrays with as many dimensions as you want.
Declaring a Multidimensional Array
A Multidimensional Array can be declared as below:
//Declares a two-dimensional array int[,] twoDimenArray; //Declares a three-dimensional array int[, ,] threeDimenArray;
Initializing a Multidimensional Array
Arrays can be initialized with default values or with some fixed values.
Initializing a multidimensional array with default values can be done as below:
//Declares and initializes a two-dimensional array to default values int[,] twoDArray = new int[2, 3]; //Declares and initializes a three-dimensional array to default values int[, ,] threeDArray = new int[2, 3, 4];
Once initialized the default value for numeric type array element is zero and for reference type array element is null. |
Multidimensional arrays can also be initialized with fixed values.
//Declares and initializes a two-dimensional array to fixed values int[,] twoDArray = new int[,] { { 1, 2 }, { 2, 3 }, { 3, 4 } }; //Declares and initializes a three-dimensional array to fixed values int[, ,] threeDArray = new int[2, 2, 3] { { { 1, 2, 3 }, { 2, 3, 4 }}, { { 11, 12, 13 }, { 12, 13, 14 }} };
Arrays can also be initialized in various other ways. Below example shows how this can be done for two-dimensional arrays.
//Declares and initializes a two-dimensional array to fixed values int[,] twoDimenArray = new int[,] { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Fixed values for Dimensions can also be used while initializing multidimensional arrays.
//Declares and initializes a two-dimensional array to fixed values int[,] twoDimenArray = new int[3,2] { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Below example initializes a two-dimensional array without using the new operator.
//Declares and initializes a two-dimensional array to fixed values int[,] twoDimenArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Accessing Multidimensional Array
To access the values of a multidimensional array the indices should be used separated by commas.
int[,] twoDimenArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } }; Console.WriteLine(twoDimenArray[0,0]); Console.WriteLine(twoDimenArray[0,1]); Console.WriteLine(twoDimenArray[1,0]); Console.WriteLine(twoDimenArray[1,1]);
Output:
1
2
2
3
2
2
3
C# Jagged Arrays
Arrays which contain other arrays as its element are called as Jagged Arrays. Also popularly called as an Array of Arrays the elements of jagged arrays can vary in dimension and size.
Declaring a Jagged Array
Jagged arrays are declared in a little different way compared to other type of arrays.
string[][] jagArray;
Initializing a Jagged Array
Jagged arrays can be initialized as below. Below array can contain two elements.
string[][] jagArray = new string[2][];
Since a Jagged array is an Array of Arrays so each of its elements should also be initialized. In the below code the first element of the jagged array contains only one element and the second element contains two elements.
jagArray[0] = new string[1]; jagArray[1] = new string[3];
Jagged arrays can also be initialized and assigned static values. The below jagged array contains two elements. The first element is an array with two elements and the second element is an array with one element.
string[][] jagArray = new string[][] { new string[]{"A","B"}, new string[]{"C"}};
Accessing Jagged Array
Values of the above jagged array can be accessed as below.
Console.WriteLine(jagArray[1][0]);
Output:
C