在vb.net中声明多个变量,只有一个“Dim”或多个,有什么区别?

时间:2015-07-13 02:36:05

标签: vb.net

简单的问题,希望是一个简单的答案。

有什么区别:

Dim something As String = "Hello"
Dim somethingElse As String = "World"
Dim putittogether As String = something & " " & somethingElse

Dim something As String = "Hello",
    somethingElse As String = "World",
    putittogether As String = something & " " & somethingElse

我知道典型的多个声明,比如......

Dim start, end As DateTime

对我的第一个和第二个例子更有好奇,好处,没有好处?没关系?

1 个答案:

答案 0 :(得分:1)

为了好玩,我通过ILDASM运行了两个版本,看看编译后是否有任何差异。如您所见,输出IL中没有任何差异。

第一个示例 - 单独的Dim语句

Dim something As String = "Hello"
Dim somethingElse As String = "World"
Dim putittogether As String = something & " " & somethingElse

编译为:

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       28 (0x1c)
  .maxstack  3
  .locals init ([0] string putittogether,
           [1] string something,
           [2] string somethingElse)
  IL_0000:  nop
  IL_0001:  ldstr      "Hello"
  IL_0006:  stloc.1
  IL_0007:  ldstr      "World"
  IL_000c:  stloc.2
  IL_000d:  ldloc.1
  IL_000e:  ldstr      " "
  IL_0013:  ldloc.2
  IL_0014:  call       string [mscorlib]System.String::Concat(string,
                                                              string,
                                                              string)
  IL_0019:  stloc.0
  IL_001a:  nop
  IL_001b:  ret
} // end of method Module1::Main

第二个例子 - 所有一行

Dim something As String = "Hello",
somethingElse As String = "World",
putittogether As String = something & " " & somethingElse

汇编为:

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       28 (0x1c)
  .maxstack  3
  .locals init ([0] string putittogether,
           [1] string something,
           [2] string somethingElse)
  IL_0000:  nop
  IL_0001:  ldstr      "Hello"
  IL_0006:  stloc.1
  IL_0007:  ldstr      "World"
  IL_000c:  stloc.2
  IL_000d:  ldloc.1
  IL_000e:  ldstr      " "
  IL_0013:  ldloc.2
  IL_0014:  call       string [mscorlib]System.String::Concat(string,
                                                              string,
                                                              string)
  IL_0019:  stloc.0
  IL_001a:  nop
  IL_001b:  ret
} // end of method Module1::Main