#125 – Initializing Multidimensional Arrays

A multidimensional array can also be initialized at the time that it is declared and instantiated, using an array initialization expression.

 byte[,] fourRGBValues = new byte[4, 3] { {0, 255, 0},
                                          {255, 0, 0},
                                          {0, 0, 255},
                                          {255, 255, 255} };

As with one-dimensional arrays, you can leave off the array sizes after the new operator because the size of the new array can be inferred.

 byte[,] fourRGBValues = new byte[,] { {0, 255, 0},
                                       {255, 0, 0},
                                       {0, 0, 255},
                                       {255, 255, 255} };

You can even leave off the new operator entirely.

 byte[,] fourRGBValues = { {0, 255, 0},
                           {255, 0, 0},
                           {0, 0, 255},
                           {255, 255, 255} };
Advertisements