tags:

views:

264

answers:

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

I need to have a 3x3 array arrangement to save information to. How do I declare this in C#?

+18  A: 
 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"} };
mumtaz
Or `var tablero = new string[3,3];` if you're using C# 3 or later
bdukes
If you use the array initializer syntax you don't need to provide the bounds (i.e. you can just write `new string[,]`)
bdukes
+4  A: 

try this :

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

have a look on http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

anishmarokey
+10  A: 

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"}
};
0xA3
+3  A: 

I assume you're looking for this:

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

The syntax for a jagged array is:

        string[][] Tablero = new string[3][];
        for (int ix = 0; ix < 3; ++ix) {
            Tablero[ix] = new string[3];
        }
Hans Passant
+1  A: 
string[,] Tablero = new string[3,3];
Chris Almond
A: 

When you are trying to create a multi-dimensional array all you need to do is add a comma to the declaration like so: string[,] tablero = new string[3,3].

hav2play21
A: 

Hi Sergio, There are many examples on working with arrays in C# here.

I hope this helps.

Thanks, Damian

Damian Schenkelman
A: 

string[][] is not a two-dimensional array, it's an array of arrays (a jagged array). That's something different.

To declare a two-dimensional array, use this syntax:

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

If you really want a jagged array, you need to initialize it like this:

string[][] tablero = new string[][] { new string[3], 
                                      new string[3], 
                                      new string[3] };
Heinzi
A: 

You just declared a jugged array. Such kind of arrays can have diferent 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: http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Bashir Magomedov