#124 – Declaring and Instantiating Multidimensional Arrays

C# allows declaring arrays of more than one dimension.  Where a 1-dimensional array can be thought of as a simple list of elements, a 2-dimensional array can be thought of as a collection of elements organized into rows and columns.

Here are a couple of examples (remember that all arrays are 0-based):

 // 2-dimensional array
 int[,] hourlyTempsForWeek = new int[7, 24];
 hourlyTempsForWeek[2, 12] = 45;     // Tuesday, 12PM
 hourlyTempsForWeek[6, 23] = 30;     // Saturday, 11PM

 // 3-dimensional array, R/G/B values for each pixel on screen
 byte[, ,] pixelRGBValues = new byte[1024, 768, 3];
 pixelRGBValues[0, 0, 0] = 255;  // R value at (0,0)
 pixelRGBValues[0, 0, 1] = 0;    // G value at (0,0)
 pixelRGBValues[0, 0, 2] = 255;  // B value at (0,0)
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #124 – Declaring and Instantiating Multidimensional Arrays

  1. Pingback: #1,016 – Retrieving the Length of an Array | 2,000 Things You Should Know About C#

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: