#127 – Declaring and Instantiating Jagged Arrays

It’s possible in C# to declare and instantiate jagged arrays. A jagged array is one that can have a different number of columns in each row (assuming the array is two-dimensional).  A jagged array can also be considered to be an array of arrays.

When you declare and instantiate a jagged array, you specify the size of only the first dimension.

// Jagged array - 3 elements, each of which is array of int
 int [][] nums2 = new int[3][];

After initialization, the jagged array consists of a one-dimensional array of arrays, with each element of the array initialized to null.

As a separate step, you can then initialize each element of the jagged array as a separate array, each of which can be a different size.

 nums2[0] = new int[4];
 nums2[1] = new int[2];
 nums2[2] = new int[10];

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