6

I have seen people use a couple of different way of initializing arrays:

string[] Meal = new string[]{"Roast beef", "Salami", "Turkey", "Ham", "Pastrami"};

or another way, also called initializing is:

string[] Meats = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

What is the best way, and what is the major difference between both ways (including memory allocation)?

Servy
  • 202,030
  • 26
  • 332
  • 449
foo-baar
  • 1,076
  • 3
  • 17
  • 42

4 Answers4

16

There is no difference in both cases. Compiler generates the same bytecode (newarr OpCode):

public static void Main()
{
   string[] Meal = new string[] { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami"};
   string[] Meats = { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };
}

MSIL:

.entrypoint
  // Code size       100 (0x64)
  .maxstack  3
  .locals init ([0] string[] Meal,
           [1] string[] Meats,
           [2] string[] CS$0$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.5
  IL_0002:  newarr     [mscorlib]System.String
  IL_0007:  stloc.2
  IL_0008:  ldloc.2
  IL_0009:  ldc.i4.0
  IL_000a:  ldstr      "Roast beef"
  IL_000f:  stelem.ref
  IL_0010:  ldloc.2
  IL_0011:  ldc.i4.1
  IL_0012:  ldstr      "Salami"
  IL_0017:  stelem.ref
  IL_0018:  ldloc.2
  IL_0019:  ldc.i4.2
  IL_001a:  ldstr      "Turkey"
  IL_001f:  stelem.ref
  IL_0020:  ldloc.2
  IL_0021:  ldc.i4.3
  IL_0022:  ldstr      "Ham"
  IL_0027:  stelem.ref
  IL_0028:  ldloc.2
  IL_0029:  ldc.i4.4
  IL_002a:  ldstr      "Pastrami"
  IL_002f:  stelem.ref
  IL_0030:  ldloc.2
  IL_0031:  stloc.0
  IL_0032:  ldc.i4.5
  IL_0033:  newarr     [mscorlib]System.String
  IL_0038:  stloc.2
  IL_0039:  ldloc.2
  IL_003a:  ldc.i4.0
  IL_003b:  ldstr      "Roast beef"
  IL_0040:  stelem.ref
  IL_0041:  ldloc.2
  IL_0042:  ldc.i4.1
  IL_0043:  ldstr      "Salami"
  IL_0048:  stelem.ref
  IL_0049:  ldloc.2
  IL_004a:  ldc.i4.2
  IL_004b:  ldstr      "Turkey"
  IL_0050:  stelem.ref
  IL_0051:  ldloc.2
  IL_0052:  ldc.i4.3
  IL_0053:  ldstr      "Ham"
  IL_0058:  stelem.ref
  IL_0059:  ldloc.2
  IL_005a:  ldc.i4.4
  IL_005b:  ldstr      "Pastrami"
  IL_0060:  stelem.ref
  IL_0061:  ldloc.2
  IL_0062:  stloc.1
  IL_0063:  ret
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
4

The two are identical, and will produce identical IL code. The second is simply syntactic sugar for the first.

Servy
  • 202,030
  • 26
  • 332
  • 449
3

Both of these ways are equivalent in the compiled code, choose the one you think is clearest or easiest to read.

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
2

See Eric Lippert's response to a similar question. In this specific scenario, they both produce the same compiled code and the same result. The only difference between the two syntaxes is that the second syntax can only be used in variable declaration, which means you can't use it to change the value of an existing variable. For instance:

// Compiles fine
string[] Meats = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

// Causes compilation error
Meats = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

// Also causes compilation error
string[] Meats2;
Meats2 = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

Personally, I would suggest using the second syntax for variable declarations if you're explicitly declaring the type of your variable, as so:

string[] Meats = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

I would recommend the first syntax if you tend to use the var keyword, as it makes it clearer what type you're expecting the results to be (readability) and forces the compiler to check for you as well, like so:

var Meats = new string[]{"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

In general, it's best to pick one style and be consistent.

Community
  • 1
  • 1
Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46