The syntax for array declaration is the same in C# as in Java, except for one difference – when declaring an array, Java permits putting the square brackets either in front of or behind the array's type. C# only allows the former – Putting the square brackets after an array identifier will cause a compilation error. [3]
[3] C/C++ mandates that the square brackets be behind the array type (as in int array[];).
Both the following declarations are okay in Java:
int MyArray[];
int []MyArray;
However, only int []MyArray; is legal in C#.
Other than that, the syntax for array declaration, instantiation, and initialization are identical in C# and Java as far as 1D arrays are concerned. Examples are shown below as a refresher and for reference.
Declaring a 1D array (without instantiating it):
int []MyArray1; object[] MyArray2;
It doesn't matter if the space is between the type and the square brackets, or between the square brackets and the array identifier.
Declaring and instantiating a 1D array in a single statement:
int []MyArray1 = new int[3]; object[] MyArray2 = new object[2];
The statements declare and instantiate MyArray1 as an int array with a size of 3, and MyArray2 as an object array of size 2.
Declaring, instantiating and initializing a 1D array in a single statement:
int[] MyArray1 = {1,2,3}; object[] MyArray2 = {"apple",9};
Accessing a value in a 1D array:
int Temp = MyArray1[1]; MyArray1[2] = 99;
Beyond 1D arrays, C# supports two types of multi-dimensional arrays – rectangular arrays and jagged arrays. Because Java's multi-dimensional arrays do not map directly into either category, I have chosen to compare Java's implementation with the simpler rectangular arrays.
18.190.239.166