Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

Arrays in C#, VB, C++, and Java

All programming languages have syntax which allows creating variables which refer to a set of identically-typed values.

C#:

int[] myArray = new int[3];
int[,] myTwoDimensionArray = new int[2, 3];
int[][] myJaggedArray = new int[3][];

VB:

'short' syntax:
Dim myArray(2) As Integer
Dim myTwoDimensionArray(1, 2) As Integer
Dim myJaggedArray(2)() As Integer

'long' syntax:
Dim myArray() As Integer = New Integer(2){}
Dim myTwoDimensionArray(,) As Integer = New Integer(1, 2){}
Dim myJaggedArray()() As Integer = New Integer(2)(){}

Note: Unlike other programming languages, arrays are declared specifying their upper bound in VB, not their length.

Note: VB also allows placing the array specifier after the variable type (e.g., Dim myArray As Integer() = New Integer(2){}), but only in the 'long' syntax.

Native C++:

int myArray[3];
int myTwoDimensionArray[2][3];
int **myJaggedArray = new int*[3];

C++/CLI:

array<int> ^myArray = gcnew array<int>(3);
array<int, 2> ^myTwoDimensionArray = gcnew array<int, 2>(2, 3);
array<array<int>^> ^myJaggedArray = gcnew array<array<int>^>(3);

Java:

int[] myArray = new int[3];
int[][] myTwoDimensionArray = new int[2][3];
int[][] myJaggedArray = new int[3][];

Note: Java also allows placing the array specifier after the variable name (e.g., int myArray[]).

Copyright © 1997 - 2017 Tangible Software Solutions Inc.