Powered by Blogger.

Saturday, February 15, 2014

Jagged arrays in C#.Net

In this post we will discuss about jagged arrays in C#.Net. Also you can check out my previous posts on:

- Get stored procedure source text in sql server 2008

- Validate URL using regular expression in Asp.Net

- Bind dropdownlist from enum in Asp.Net

A jagged array is an array where every row can have different size, jagged array is very much flexible in sizing. Means the first row can have 5 elements, the 2nd row can have 4 elements, the 3rd row can have 10 elements etc.

It is also called as array of arrays.

Below is a a single-dimensional integer array that has three elements.

int[][] jaggedArray = new int[3][];

And then you can define the elements for each row like below:

jaggedArray[0] = new int[3];

jaggedArray[1] = new int[2];

jaggedArray[2] = new int[5];

Here the first row will contain 3 elements, the 2nd row will contain 2 elements and the 3rd row will contain 5 elements.

Then you can initialize the value to the row like below:

jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9, 10, 11 };


Post a Comment