ArrayList 和 Array 的区别

发布于 2024-08-18 05:00:10 字数 374 浏览 5 评论 0原文

参考MSDN,指出“您可以设置数组的下界,但 ArrayList 的下界始终为零”

如果我声明一个数组 a[10],则下界始终为 a[0]。

这是那里指定的下限吗?如果是,我们如何设置数组的下界,因为数组的索引总是以[0]开头。

或者链接中规定的下限是否有所不同?

注意:我知道该链接指向.NET Framework 1.1的内容,但仍然很好奇他们到底提到了什么。

With reference to MSDN, It is stated that "You can set the lower bound of an Array, but the lower bound of an ArrayList is always zero"

If i declare an array a[10], the lower bound is always a[0].

Is this the lower bound specified there? If yes, How can we set the lower bound of an array, Since the index of an array always starts with a[0].

Or is the lower bound stated in the link is something different?

Note: I know the link point to the contents of .NET Framework 1.1 but still curious to know what exactly they have mentioned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-08-25 05:00:10

您可以使用 Array.CreateInstance

请注意,您将无法将其转换为 Foo[] (其中 Foo 是相关类型),除非您也将其设置为多维的。 CLR 中有两种类型的数组 - 向量(从零开始,单维)和数组(可以是多维且具有非零下界) 。

C# 中的 T[] 始终对应于向量,而 T[][] 对应于数组。所以你可以这样做:

int[][] rectangle = (int[][]) Array.CreateInstance(typeof(int),
                                       new int[]{2, 2}, // lengths
                                       new int[]{-1, -1}); // lower bounds

但这会失败:

int[] rectangle = (int[]) Array.CreateInstance(typeof(int),
                                       new int[]{2}, // length
                                       new int[]{-1}); // lower bound

同样,你不能将其转换为 IEnumerableIList - 尽管你可以使用 < 迭代它code>IEnumerable 就可以了。

就我个人而言,我会避免像瘟疫一样使用非零下界数组。他们工作起来很慢而且很痛苦。

You can create an array with a non-zero lowerbound using Array.CreateInstance.

Note that you won't be able to cast that to a Foo[] (where Foo is the relevant type) unless you also make it multidimensional. There are two types of array inside the CLR - a vector (zero based, single dimensional) and an array (can be multi-dimensional and have non-zero lower bound).

A T[] in C# always corresponds to a vector, whereas a T[][] corresponds to an array. So you can do:

int[][] rectangle = (int[][]) Array.CreateInstance(typeof(int),
                                       new int[]{2, 2}, // lengths
                                       new int[]{-1, -1}); // lower bounds

but this will fail:

int[] rectangle = (int[]) Array.CreateInstance(typeof(int),
                                       new int[]{2}, // length
                                       new int[]{-1}); // lower bound

Likewise you can't cast it to IEnumerable<int> or IList<int> - although you can iterate over it with IEnumerable just fine.

Personally I would avoid using non-zero lower-bounded arrays like the plague. They're slow, and painful to work with.

困倦 2024-08-25 05:00:10

C# 和 VB.NET 中的下限始终为 0。 Visual Basic 6.0 及更早版本允许变量下限。他们删除了它以重写 .NET 语言。

以下文章详细介绍了如何执行此操作: http:// msdn.microsoft.com/en-us/magazine/cc301755.aspx。查找“创建具有非零下界的数组”

The lower bound in C# and VB.NET is always at 0. Visual Basic 6.0 and older allowed for variable lower bounds. They removed it for the rewriting of the language for .NET.

Here is an article that goes into detail of how to do it: http://msdn.microsoft.com/en-us/magazine/cc301755.aspx. Look for "Creating Arrays with a Non-zero Lower Bound"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文