#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} };

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers