In actual programming, arrays are objects that are used a lot. Like Array, List, etc., they are all encapsulations of arrays.
Let’s take a look at the following two definition methods. Can we see the difference between them?
Dim tB1() As Byte
Dim tB2() As Byte = {}
Both of these define a byte array, and there are no elements in the array.
But the difference can be seen in the following two sentences.
Debug.Print(tB1 Is Nothing)
Debug.Print(tB2 Is Nothing)
The result of the first sentence is True, and the result of the second sentence is False. Note that tB1 is an array but has not been initialized, which is equivalent to Nothing. tB2 is an array. Although it has no elements, it has been initialized and is not equivalent to Nothing. You can access other properties of tB2, such as Length, and the result is 0, indicating that is an empty array. Accessing other attributes of tB1, such as Length, will result in an error.
There are still differences between empty arrays and uninitialized arrays in many places.
For example:
Debug.Print(System.Text.Encoding.Default.GetString(tB1))
Debug.Print(System.Text.Encoding.Default.GetString(tB2))
Although there are no elements in tB1 and tB2, the system will throw an exception because tB1 is not initialized. Although tB2 has no elements, it represents an array with 0 elements. The system will not throw an exception and return an empty string.
It can be seen from this that there is still a difference between Nothing and an array of 0 elements. This is something we easily overlook.