2015-07-13 192 views
1

簡單的問題,希望一個簡單的答案。在vb.net中聲明多個變量,使用一個「Dim」或多個變量,有什麼區別?

有什麼區別:

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 

更好奇我的第一和第二例如,好處,沒有好處?沒關係?

+2

沒什麼。只是一種風格選擇。 – SSS

+0

@SSS謝謝。不能更簡單。 – user1447679

回答

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 
+0

太好了。感謝您花時間做到這一點!我更喜歡使用第二個例子,不僅在vb.net中,而且在JavaScript中使用。對我來說似乎更清潔。 – user1447679

相關問題