• Saves Valuable Time
  • Trusted Accuracy Since 2004
  • 15-Day Money-Back Guarantee

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.

C++:

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

However, C++ developers are increasingly using std::vector in place of arrays, so the first array would often be rewritten as a vector:
std::vector myArray(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 © 2004 – 2024 Tangible Software Solutions, Inc.