Home > DeveloperSection > Forums > How do I declare a Jagged array of 2 dimensions?
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    September-04-2013 2:22 AM

 C# C# 
Ratings:


 1 Reply(s)
 825  View(s)
Rate this:

I m trying to declare a 2D dynamic array below is my code:

   var marray= new[,]

                        {

                            {

                                "1",

                                "Module 1.1",

                                "Module 1.2",

                                " Module 1.3",

                                "Module 1.4",

                                "Module 1.5"

                            },

                            {

                                "2",

                                "Module 2.1"                                   

                            }

                        };

I am getting error on second value "An array initializer of '6' is expected". I can understant it is expecting 2nd to be having 6 values but I need it to be dynamic of any length. I dont know much about array so cannot resolve it. Can you please guide. Thanks.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-04-2013 7:00 AM

Hi Pravesh,

You want a ragged array rather than a straight multi-dimensional array because your sub-array sizes are not the same.

Declare as

var marray= new[][]

rather than

var marray= new[,]


Don't want to miss updates? Please click the below button!

Follow MindStick