如何在C#中的2d数组中获取内部数组的长度? [英] How do I get the inner array's length in a 2d array in C#?

查看:96
本文介绍了如何在C#中的2d数组中获取内部数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我创建了一个锯齿状的二维数组,如下所示:

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

我可以用Java做到这一点.但是,当我在[] [] 2d数组的第一个括号中键入内容时,Intellisense菜单不会返回该数组的普通成员.

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.

如何在C#中获取2d数组的内部数组长度?

How do I get the inner array lengths of my 2d array in C#?

推荐答案

您发布的声明甚至不会在C#中编译,这是您的主要问题.

The declaration you posted won't even compile in C#, which is your main issue.

您已经使用 char [,] 声明了一个2D,非锯齿状的数组,但是您试图通过使用<代码> {{'0'},{'1'},{'A','B','C'}} .

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;

但是,您最初发布的代码行不会在C#中编译.

The line of code you posted at first, however, won't compile in C#.

这篇关于如何在C#中的2d数组中获取内部数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆