Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Index of Arrays in C# and VB.Net

Tags:

arrays

c#

vb.net

Have a look at the following code.,

C#

 string[] testString = new string[jobs.Count];

Equivalent VB.Net

Dim testString() As String = New String(jobs.Count - 1) {}

Why it is taking 'jobs.Count - 1' instead 'jobs.Count' in vb.net while creating new arrays?

like image 924
Sunil Avatar asked Apr 08 '13 09:04

Sunil


2 Answers

In VB.NET the number in the array declaration means "max index", but in C# it means "number of elements"

like image 130
fixagon Avatar answered Nov 15 '22 22:11

fixagon


In C# the array has the number of elements you provide:

string[] array = new string[2]; // will have two element [0] and [1]

In VB.NET the array has the number of elements you provide, plus one (you specify the max index value):

Dim array(2) As String // will have three elements (0), (1) and (2)
like image 39
John Willemse Avatar answered Nov 15 '22 21:11

John Willemse