C # ranks number dimension and get a multidimensional array initialization problem

C # novice, before you write a program to find the Internet a lot, found on this issue, the Internet's answer was wrong to have their own experiment a bit, in this recording to prevent yourself forget.

1.

 int[,] a = new int[4,5];
 System.Console.WriteLine("a.Rank = " + a.Rank);
 System.Console.WriteLine("a.GetLength(0) = " + a.GetLength(0));
 System.Console.WriteLine("a.GetLength(1) = " + a.GetLength(1));

Here is the final output

a.Rank = 2
a.GetLength(0) = 4
a.GetLength(1) = 5

You can see, Rank is the number of property acquired dimension instead of rows, Getlength () method to get the dimensions specified length, depth here on the ranks of the argument can be unified into said first length, the second length dimension, to avoid confusion .

2.

C # in a multi-dimensional array, is forced to rectangular or cuboid, that is to say each dimension "line" must be the same as the length of each

int[,] b;
b  = new int[,] { { 1, 2 }, { 2 } };

This initialization will be excluded exception.

Published 19 original articles · won praise 2 · Views 5176

Guess you like

Origin blog.csdn.net/gunjiu4462/article/details/81234412