声明变量(作为新变量)然后初始化并直接初始化它之间的区别是什么?

时间:2013-04-19 14:16:11

标签: c# asp.net .net

我见过人们使用几种不同的初始化数组的方式:

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

或其他方式,也称为初始化:

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

最好的方法是什么,两种方式(包括内存分配)之间的主要区别是什么?

4 个答案:

答案 0 :(得分:16)

两种情况都没有区别。编译器生成相同的字节码(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

答案 1 :(得分:4)

这两个是相同的,将产生相同的IL代码。第二个是第一个语法糖。

答案 2 :(得分:3)

这两种方式在编译代码中是等效的,选择您认为最清楚或最容易阅读的方法。

答案 3 :(得分:2)

Eric Lippert's response to a similar question。在这个特定的场景中,它们都产生相同的编译代码和相同的结果。两种语法之间的唯一区别是第二种语法只能用于变量声明,这意味着您不能使用它来更改现有变量的值。例如:

// 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" };

就个人而言,如果您明确声明变量的类型,我会建议使用第二种语法进行变量声明,如下所示:

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

如果您倾向于使用var关键字,我建议使用第一种语法,因为它可以更清楚地显示您期望结果的类型(可读性)并强制编译器检查您,像这样:

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

一般来说,最好选择一种风格并保持一致。