Reputation: 991
Let's say I create a jagged 2d array like so:
public static char[,] phoneLetterMapping = { {'0'}, {'1'}, {'A', 'B', 'C'} };
Now, given the first index of the array, I would like to determine the length of the inner array.
I would like to be able to do something like:
phoneLetterMapping[2].length
I can do that in Java. But the Intellisense menu doesn't return the normal members of an array when I type in the first bracket of the [][] 2d array.
How do I get the inner array lengths of my 2d array in C#?
Upvotes: 1
Views: 2028
Reputation: 60065
In C# there are two types of 2d arrays:
real 2d array and jagged (array of arrays)
in your case you used syntax for 2d array and if you want to get it's length you need to call GetLength and specify dimension. In your case it is 1 (0 is first) really your code will not compile, because your array is not rectangular - it is a must for 2d array.
if you want a jabbed array see this: http://msdn.microsoft.com/en-us/library/2s05feca.aspx
Upvotes: 1
Reputation: 16505
The declaration you posted won't even compile in C#, which is your main issue.
You've declared, using char[,]
a 2D, non-jagged array, but you're trying to instantiate a jagged array on the right side of the "=" by using { {'0'}, {'1'}, {'A', 'B', 'C'} }
.
Now, if you declare it differently:
public static char[][] phoneLetterMapping = { new[] { '0' }, new[] { '1' }, new[] { 'A', 'B', 'C' } };
Then you can simply call something like:
phoneLetterMapping[0].Length;
The line of code you posted at first, however, won't compile in C#.
Upvotes: 4
Reputation: 6433
Youre looking for GetLength
phoneLetterMapping.GetLength(dimension)
Upvotes: 1