87
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#?

  • 3
    The documentation clearly shows how to do it: http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx – Esteban Araya Sep 28 '10 at 15:14
  • Also see [c-how-to-initialize-an-array-of-2d-arrays](http://stackoverflow.com/questions/7775458/c-how-to-initialize-an-array-of-2d-arrays) – nawfal Oct 13 '13 at 15:03
  • 1
    Note that there is a difference between the OP code [][] (jagged array structure) and the accepted answer [,] (multidimensional), according to the link above. – amalgamate Apr 04 '16 at 14:08

12 Answers12

118
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"} };
evandrix
  • 6,041
  • 4
  • 27
  • 38
explorer
  • 11,710
  • 5
  • 32
  • 39
  • 2
    Or `var tablero = new string[3,3];` if you're using C# 3 or later – bdukes Sep 28 '10 at 15:09
  • 9
    If you use the array initializer syntax you don't need to provide the bounds (i.e. you can just write `new string[,]`) – bdukes Sep 28 '10 at 15:16
  • 2
    You don't need to write `new string[,]` at all ... `string [,] Tablero = {{"a","b","c"}, {"d","e","f"}, {"g","h","i"} }` – Jim Balter Dec 28 '16 at 23:27
  • 4
    To clarify that first 3 in [3, 3] is the number of rows, the second 3 is the number of columns (everyone seems to have picked [3, 3] just to confuse :-)) – bytedev Jun 15 '17 at 15:38
61

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"},
    new string[] {"2.1", "2.2", "2.3", "2.4"},
    new string[] {"3.1", "3.2", "3.3"}
};
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
18

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

nawfal
  • 70,104
  • 56
  • 326
  • 368
Bashir Magomedov
  • 2,841
  • 4
  • 28
  • 44
12

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
  • 922,412
  • 146
  • 1,693
  • 2,536
10

There are 2 types of multidimensional arrays in C#, called Multidimensional and Jagged.

For multidimensional you can by:

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

For jagged array you have to write a bit more code:

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

In short jagged array is both faster and has intuitive syntax. For more information see: this Stackoverflow question

Community
  • 1
  • 1
Lev
  • 3,719
  • 6
  • 41
  • 56
  • 3
    In the jagged case you can initialize it directly like `string[][] jagged = { new[] { "a", "b", "c" }, new[] { "d", "e", "f" }, new[] { "g", "h", "i" } };` **or** `var jagged = new[] { new[] { "a", "b", "c" }, new[] { "d", "e", "f" }, new[] { "g", "h", "i" } };` – nawfal Oct 13 '13 at 14:27
9

try this :

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

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

bdukes
  • 152,002
  • 23
  • 148
  • 175
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • This is a relevant article. The thing I can't stand about a lot of msdocs documentation is the use of just simple integers for examples. They should at least have a few other examples, especially one with strings. – Su Llewellyn Sep 15 '21 at 18:06
6

A 3x3 (multidimensional) array can also be initialized (you have already declared it) like this:

string[,] Tablero =  {
                        { "a", "b", "c" },
                        { "d", "e", "f" }, 
                        { "g", "h", "i"} 
                     };
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    -1: this is the same as http://stackoverflow.com/a/3814169/76337 and http://stackoverflow.com/a/3814164/76337 – John Saunders Oct 13 '13 at 13:54
  • @JohnSaunders I fail to see. Leaving the entire type ignored on the rhs is something different. – nawfal Oct 13 '13 at 13:56
  • 1
    No, it's exactly the same. – John Saunders Oct 13 '13 at 13:56
  • 1
    @JohnSaunders I was suggesting a different way to initialize. Not many know that she can omit the size and even type on rhs during array initialization. Of course it does the same. Since the question is more like "how do I declare/initialize", this is one way too. See this Eric's [answer](http://stackoverflow.com/a/5678393/661933) in which he separately mentions each technique. – nawfal Oct 13 '13 at 14:00
  • 2
    Oh, ok. I would get rid of my downvote, but it seems to have disappeared already. – John Saunders Oct 13 '13 at 14:29
  • @JohnSaunders I was never downvoted though you claim to have made so :) may be network error.. – nawfal Oct 13 '13 at 14:31
6
string[,] Tablero = new string[3,3];
Chris Almond
  • 577
  • 3
  • 6
6

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].
nawfal
  • 70,104
  • 56
  • 326
  • 368
hav2play21
  • 171
  • 7
6

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
  • 167,459
  • 57
  • 363
  • 519
3

you can also write the code below.

Array lbl_array = Array.CreateInstance(typeof(string), i, j);

where 'i' is the number of rows and 'j' is the number of columns. using the 'typeof(..)' method you can choose the type of your array i.e. int, string, double

evandrix
  • 6,041
  • 4
  • 27
  • 38
Stavros Afxentis
  • 289
  • 1
  • 4
  • 16
-1

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

I hope this helps.

Thanks, Damian

Damian Schenkelman
  • 3,505
  • 1
  • 15
  • 19