数组和列表长度未初始化
Array and List length isn't as initialized
好吧,我很困惑。我写了一段时间的代码,但从未见过这个。
当我像这样在 VB.NET
中创建数组时
Dim arr(2) as Object
然后自己获取长度(调试器或代码),它 returns 3
。
当我创建一个长度为 0
的数组时,它的大小为 1
.
当我创建一个 List (new List(1) -> count:2
)
时,也会发生同样的情况
这有什么意义。我以前从未在任何其他语言中看到过这种行为......
有没有人对此有解释。
谢谢。
答案在docs
When you use Visual Basic syntax to define the size of an array, you
specify its highest index, not the total number of elements in the
array.
Dim arr(2)
创建一个数组,最后一个索引为 2,这意味着索引 0-2 有 3 个位置。
好吧,我很困惑。我写了一段时间的代码,但从未见过这个。
当我像这样在 VB.NET
中创建数组时
Dim arr(2) as Object
然后自己获取长度(调试器或代码),它 returns 3
。
当我创建一个长度为 0
的数组时,它的大小为 1
.
当我创建一个 List (new List(1) -> count:2
)
这有什么意义。我以前从未在任何其他语言中看到过这种行为......
有没有人对此有解释。
谢谢。
答案在docs
When you use Visual Basic syntax to define the size of an array, you specify its highest index, not the total number of elements in the array.
Dim arr(2)
创建一个数组,最后一个索引为 2,这意味着索引 0-2 有 3 个位置。