VB.NET Dim vs. New

折月煮酒 提交于 2019-12-06 05:52:37

They both allocate 20 bytes on the managed heap.

They both set the identifier 'byteArray' to point to those bytes.

The statement with the "new" operator (clause) allows initialization of the array elements.


Dim byteArray() as Byte = new Byte(20) { 1, 2, 3, 4, 5, 6, ... }

Incidentally, to allocate an array with no elements specifiy a size of -1 for one of the dimensions. This is useful if you need to access properties like length without throwing an error.

It's the same thing.

Always more than 1 way to skin a cat.

Yup, the same. The 2nd statement is one to avoid, few would guess that it actually creates an array with 21 elements. Not that it is that obvious from the 1st statement either...

Both are the same.

Dim byteArray(20) as Byte will create an array with 21 elements

Dim byteArray() as Byte = new Byte(20) {} will create an array with 20 element

There's no difference. Redim is carryover syntax that vb 6 developers are familiar with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!