Saturday 28 June 2008

Array declaration in C# v VB.NET

One particular small thing that I prefer in C# over VB.NET is the way arrays are declared. In C# you declare the size of the array with the total number of elements, instead of the upper bound.

i.e. To declare an array of 10 integers in C#:

   1:  int[] myArray = new int[10];


To declare an array of 10 integers in VB:

   1:  Private myArray(9) As Integer


Other obvious differences are the use of square brackets for C# instead of the parentheses used with VB. Also, th array brackets are part of the type description, instead of the variable name.

In my opinion, the C# declaration seems more correct to me, and it has meant I am using less of the 'Array.Length - 1' syntax when using loops etc.


This is a good article in C# Corner that explains the use of arrays.

0 comments: