+ 4
C# array length is all elements?
I have just tried this example in the course on multidimensional arrays in C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int[, ] arr = {{2, 4, 7},{5, 5, 4}}; Console.WriteLine(arr.Length); Console.WriteLine(arr.Rank); } } } And I am surprised that the length is 6 đ±. How come? I was expecting 2. If I wanted to print it as table, and keep it flexible so that it adjusts with changes in the array sizes, is it customary to use arr.Length/arr.Rank, or is there a better way? Thank you đ€đ
1 Answer
+ 1
Yes, i suppose i was not very clear đ€đ€ At first, I was only surprised, that the length is 6. In the lesson they wrote that a multidimensional array is really just one big block of elements. Maybe that is the reason? đ€đ€·
Then I wondered how to know what the array looks like. At first i thought that Rank solves the problem. Like you said. Length is 6, Rank is 2, so Length/Rank is 3, which is the number of elements in a row đ
But what if the Rank is 3? Or even higher? Then this kind of calculation starts to get difficult đ° But now i just realised that it is worse! Because an array with three rows and two columns also has length 6 and rank 2! Just like the array with two rows and three columns.
I seem to lose necessary information about the structure đ€đ