7

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?

1
  • 2
    Please review your question. Both examples do not make sense. They are both not defined as arrays. Commented Apr 8, 2013 at 9:32

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

Comments

6

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)

Comments

2

Because with your C# code sample,

string testString = new string[jobs.Count];

That's a constructor of creating an array of string.

While with the VB.Net example,

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

You are referring with a new String object with length of string declared in the parenthesis.

If you want to create an array of String in VB.Net it must be like this:

Dim testString (jobs.Count) As String

see supporting links below: VB.Net C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.