How can I declare a two dimensional string array? How can I declare a two dimensional string array? arrays arrays

How can I declare a two dimensional string array?


string[,] Tablero = new string[3,3];

You can also instantiate it in the same line with array initializer syntax as follows:

string[,] Tablero = new string[3, 3] {{"a","b","c"},                                      {"d","e","f"},                                       {"g","h","i"} };


You probably want this:

string[,] Tablero = new string[3,3];

This will create you a matrix-like array where all rows have the same length.

The array in your sample is a so-called jagged array, i.e. an array of arrays where the elements can be of different size. A jagged array would have to be created in a different way:

string[][] Tablero = new string[3][];for (int i = 0; i < Tablero.GetLength(0); i++){    Tablero[i] = new string[3];}

You can also use initializers to fill the array elements with data:

string[,] Tablero = new string[,]{    {"1.1","1.2", "1.3"},    {"2.1","2.2", "2.3"},    {"3.1", "3.2", "3.3"}};

And in case of a jagged array:

string[][] Tablero = new string[][]{    new string[] {"1.1","1.2", "1.3"},    new string[] {"2.1","2.2", "2.3"},    new string[] {"3.1", "3.2", "3.3"}};


You just declared a jagged array. Such kind of arrays can have different sizes for all dimensions. For example:

string[][] jaggedStrings =  {new string[] {"x","y","z"},new string[] {"x","y"},new string[] {"x"}};

In your case you need regular array. See answers above.More about jagged arrays