HOW DO I DECLARE A JAGGED ARRAY OF 2 DIMENSIONS?

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
C# 
C#
 1068  View(s)
Ratings:
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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: How do I declare a Jagged array of 2 dimensions?

    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[,]

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!