Dominiek
Dominiek

Reputation: 107

VB.NET Array Initialization confuses me

Coming from a C# background, I have no idea why the following declarationr returns an array with length = 2, can someone please enlighten me?

Dim lTestArray(1) As String

Console.WriteLine(lTestArray.Length) (writes 2)

Upvotes: 2

Views: 768

Answers (2)

Nick
Nick

Reputation: 5955

In VB.NET, you don't specify the length of the array... you actually specify the index of the last addressable element. Since .NET arrays are 0 based, and you specified 1 to be the last indexable element, the length is 2.

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185643

VB.NET array declarations supply the upper bounds (i.e. the maximum index) of the array, not the length. Since arrays are 0-based, a maximum index of 1 gives you two elements (0 and 1).

Upvotes: 3

Related Questions