简体   繁体   中英

How do I declare a Jagged array of 2 dimensions?

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

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

Refs:

You're receiving the compile error because your sub-array sizes are not equivalent. You'll either need to implement this using jagged arrays as Mitch Wheat suggests or ensure that all elements in the sub-array have the same number of elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM