Initialize int[] (or any other array of objects)

  • Thread starter Thread starter Iwan
  • Start date Start date
I

Iwan

Hi,

What is the best way to initialize an new integer array?
(performance, readability, etc..)

int[] integers = {1, 2, 3};

or

int[] integers = new int[] {1, 2, 3};


note: first syntax will fail when assigning a value to the array

int[] integers;
integers = new int[] {1, 2, 3}; // OK
integers = {1, 2, 3}; // Error

greetz,
Iwan Bel
 
Iwan said:
What is the best way to initialize an new integer array?
(performance, readability, etc..)

int[] integers = {1, 2, 3};

or

int[] integers = new int[] {1, 2, 3};

They are equivalent - they end up as the same IL.
 
The first is more readable.


Jon Skeet said:
Iwan said:
What is the best way to initialize an new integer array?
(performance, readability, etc..)

int[] integers = {1, 2, 3};

or

int[] integers = new int[] {1, 2, 3};

They are equivalent - they end up as the same IL.
 
Back
Top