ArrayList 和 Array 的区别
参考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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Array.CreateInstance
。请注意,您将无法将其转换为
Foo[]
(其中Foo
是相关类型),除非您也将其设置为多维的。 CLR 中有两种类型的数组 - 向量(从零开始,单维)和数组(可以是多维且具有非零下界) 。C# 中的
T[]
始终对应于向量,而T[][]
对应于数组。所以你可以这样做:但这会失败:
同样,你不能将其转换为
IEnumerable
或IList
- 尽管你可以使用 < 迭代它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[]
(whereFoo
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 aT[][]
corresponds to an array. So you can do:but this will fail:
Likewise you can't cast it to
IEnumerable<int>
orIList<int>
- although you can iterate over it withIEnumerable
just fine.Personally I would avoid using non-zero lower-bounded arrays like the plague. They're slow, and painful to work with.
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"