O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.1. One-dimensional arrays

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required