-2

Why define this

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

When you can define like this?

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

Any thoughts?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
WTFZane
  • 592
  • 1
  • 4
  • 25
  • 2
    The latter is just syntactic sugar for the former. https://msdn.microsoft.com/en-us/library/aa664573(v=vs.71).aspx – Tim Schmelter Nov 11 '15 at 11:49
  • Can you please explain that can be understand by beginners? – WTFZane Nov 11 '15 at 11:50
  • Syntactic Sugar is just functionallity that the compiler provides to allow you to write things in an easier/shorter way. The code actually compiled is similar to your second option – Juan Nov 11 '15 at 11:52
  • Essentially, they do the same thing, pick your favourite – Sayse Nov 11 '15 at 11:52
  • Another option: `var array = new [] {1,2,3};` They all do the same thing. – Dennis_E Nov 11 '15 at 11:58
  • But why?! Why does the creator of c# needs to have this "Syntactic sugar"? – WTFZane Nov 11 '15 at 12:07
  • To help out code optimizers/analyzers. (Not really though.) – Fᴀʀʜᴀɴ Aɴᴀᴍ Nov 11 '15 at 12:08
  • to let developers write code faster – Val Nov 11 '15 at 12:13
  • Because more options will help developers who are familiar in a different language to learn the ropes of C#. A beginner might want to use `int[] array = new int[]...` so s/he can see exactly what's going on. Later, as a more experienced developer you might want o use `var array = new[]...` because you already know what's going on and it's just easier to type. – waka Nov 11 '15 at 14:09

1 Answers1

1

Feel like taking risk to answer that but..

From $12.6 Array initializers in C# 5.0 Language Spec;

The context in which an array initializer is used determines the type of the array being initialized. In an array creation expression, the array type immediately precedes the initializer, or is inferred from the expressions in the array initializer. In a field or variable declaration, the array type is the type of the field or variable being declared. When an array initializer is used in a field or variable declaration, such as:

int[] a = {0, 2, 4, 6, 8};

it is simply shorthand for an equivalent array creation expression:

int[] a = new int[] {0, 2, 4, 6, 8};

As other says, this can be called a syntactic sugar for this initializers.

But why?! Why does the creator of c# needs to have this "Syntactic sugar"?

Wikipedia page states this as well.

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

So, what is the proper method? Well, that question is subjective. Some may prefer first, some may prefer second, or even some may prefer var array = new[] { 1, 2, 3 }; based on your example. var is also a syntactic sugar which comes C# 3.0 version. But with this, you can not write var array = { 1, 2, 3 }; since it can complicate the parser as Eric Lippert noted.

By the way, you can find All possible C# array initialization syntaxes in here and What is the difference between “Syntax” and “Syntactic Sugar” in Programming.SE.

Also I have to write this lovely comment by Anthony Pegram in Programming.SE;

In the end, it's all just syntatic sugar over electricity.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364