Thursday, 15 October 2015

Multidimensional Array in C#

Multidimensional arrays (rectangular arrays)

Arrays can have more than one dimension; these arrays-of-arrays are called multidimensional arrays. They are very similar to standard arrays with the exception that they have multiple sets of square brackets after the array identifier.

The most common kind of multidimensional array is the two-dimensional array. A two dimensional array can be thought of as a grid of rows and columns.

Declaring multidimensional array


string[,] names = new string[4, 4];
int[,] numbers = new int[3, 4];

Multidimensional array initialization


int[,] numbers = new int[3, 2] {{1,2}, {3,4}, {5,6}};
string[,] names = new string[2, 2] {{"Ana""Anita"}, {"Bob""Barbie"}};

You can omit the size of the array, like this:

int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[,] {{"Ana""Anita"}, {"Bob""Barbie"}};

You can also omit the new operator if you initialize array like this:

int[,] numbers = {{1,2}, {3,4}, {5,6}};
string[,] names = {{"Ana""Anita"}, {"Bob""Barbie"}};


No comments:

Post a Comment