Howdy. I am very new to C# so I please forgive my ignorance. I was reading about jagged arrays and multidimensional arrays.

I am trying to understand why the following code will work as I would expect it to.

int[][] intJaggedArray = new int[5][];
intJaggedArray[0] = new int[5];
Console.WriteLine(intJaggedArray[0].Length);

Where this code will produce an error

int[,] intMultiArray = new int[5,5];
Console.WriteLine(intMultiArray[0].Length);

Is a multidimensional array not an array of arrays (of fixed length)? I understand that I should already know that the length is going to be 5 (I did declare it so after all) but I just seek a deeper understanding of what makes it an invalid statement.

Thank you,
-Ninwa

I have discovered that the following will work:

Console.WriteLine( intMultiArray.GetLength(0) );

I guess it's just one of those syntactical gotchas.

I think that my confusion is stemmed in my C++ background of thinking about arrays as pointers to the first memory address of a series of allocations in memory. Multidimensional arrays in C++ are taught as an array of pointers to several arrays of fixed length. In C# this is not the case, a multidimensional array is simply x by y values of the type, no arrays of arrays or pointers.

in a way all c# stuff is pointers as everything (including integers etc) are objects, but at the same time, you basically ignore that. So yes, it can be confusing from a c++ point of view.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.