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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top