9

Simple Short Question What exactly is the difference between

int[] intarray = new int[2]{1,2};

and

int[] intarray2 = {4,5,6};

Like what does the "new" do exactly? and do you really need it?

Im guessing it just allocates memory.....or something? Sorry im completely new to C# and have questions as I go about learning it.

5 Answers 5

12

The second is array initializer syntax. It's just syntactic sugar. Both initialize new array instances with their respective values, the first being more explicit (you repeat that it is an integer array of two elements on the right hand side which is pretty obvious from what follows so the compiler is capable of inferring this information).

So the following are equivalent:

int[] array = new int[2] { 1, 2 };

and:

int[] array = { 1, 2 };

and:

var array = new[] { 1, 2 };

In all cases we are initializing an array of two integers. Personally I prefer the last syntax.

Sign up to request clarification or add additional context in comments.

4 Comments

What exactly is synatactic sugar. I've never heard that term before?
Syntactic sugar is something that is built into the language and simplifies the syntax but doesn't actually exist in the compiled assembly. When you look at the generated IL there's the first much more explicit syntax. But when writing it is better if we leave the compiler do the job instead of us. Why use the first syntax? Why repeat int on the right hand side? Why repeat the size of the array? All this is pretty obvious when we simply write {1, 2}. From this we know that there are two elements and that the type is int.
Good explanation. I think the most important point is that syntactic sugar allows us to express an idea in a terse fashion yet still have a valid program.
The form {4,5,6} generates compile time errors when it is passed to a function as an argument or returned from a function. The new keyword and brackets are required in these scenarios.
2

Arrays are reference types and are allocated using the C# new keyword as are other reference types.

One of your array syntaxes is simply a shorter syntax also recognized by the C# compiler - (there are other variations on it too) Both your examples allocate (new) and initialize {elements} for an array instance:

The first version explicitly states the size of the array.

int[] intarray = new int[2]{1,2};

The second version lets the C# compiler infer the size of the array by # of elements in the initialization list.

int[] intarray2 = {4,5,6};

In general the C# compiler recognizes specialized syntax for declaring and initializing C# native arrays masking the fact the underlying System.Array class is being used.

Comments

1
int[] intarray2 = {4,5,6};

is exactly the same than:

int[] intarray2 = new int[] {4, 5, 6};

is just a shorter (abbreviated) form of coding, in fact the compiler WILL allocate the memory the same qty of memory for the array, the first one is not explicitly write the "new", just like when you assign an integer to a double variable, you can write the cast or not (because is a safe cast), and will produce the same effect. example:

int i = 12;
double d = i;

the same as:

int i = 12;
double d = (double)i;

Comments

0

This is a syntactic sugar.

There is no difference between them; they will compile to identical IL.
However, the array initializier can only be used to initialize array fields or properties.

Comments

0

http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx

Acc. to this, new will just call the default constructor.

int i=new int();

OR

int i=0;

are same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.