Starting Array Index in C # and VB.Net

Look at the following code.,

FROM#

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

Equivalent to VB.Net

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

Why instead of "jobs.Count - 1" instead of "jobs.Count" in vb.net when creating new arrays?

+7
source share
3 answers

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

+13
source

In C #, an array contains the number of elements you provide:

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

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

 Dim array(2) As String // will have three elements (0), (1) and (2) 
+4
source

Since your sample code is C# ,

 string testString = new string[jobs.Count]; 

This is a constructor for creating a string array.

While with the example of VB.Net

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

You are referencing a new String object with the length of the string declared in parentheses.

If you want to create an array from String in VB.Net, it should look like this:

 Dim testString (jobs.Count) As String 

see support links below: VB.Net C #

+2
source

All Articles