What are the differences between the following constructs? Why prefer one over the other?
Dim byteArray(20) as Byte
and
Dim byteArray() as Byte = new Byte(20) {}
Edit - Corrected the ReDim. Should be a Dim.
What are the differences between the following constructs? Why prefer one over the other?
Dim byteArray(20) as Byte
and
Dim byteArray() as Byte = new Byte(20) {}
Edit - Corrected the ReDim. Should be a Dim.
There's no difference. Redim is carryover syntax that vb 6 developers are familiar with.
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.
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...