VB.NET使用局部變量簡介
VB.NET有很多值得學(xué)習(xí)的地方,這里我們主要介紹VB.NET使用局部變量,包括介紹null引用異常等方面。
VB.NET使用局部變量
我們知道,在類或者結(jié)構(gòu)初始化的時(shí)候,成員變量***不要顯式賦以0、Nothing、False等初值,這樣不但沒有必要而且會降低性能。因?yàn)槌蓡T變量首先會被分配內(nèi)存空間,并且該內(nèi)存空間自動(dòng)用“0”進(jìn)行初始化,因此顯式的賦值會增加指令操作而影響性能。
但如果是局部變量呢?
一般情況下,如果不顯式初始化局部變量,局部變量會被自動(dòng)也賦以空值、0、或者false.
- '例如以下代碼
 - Dim b As Boolean
 - Console.WriteLine(b)
 - Dim rnd1 As Random
 - If rnd1 Is Nothing Then
 - Console.WriteLine("yes")
 - End If
 - '最終會輸出:"false yes"
 
不過對于引用類型的Random,編譯器會給出“ 變量”rnd1“在賦值前被使用??赡軙谶\(yùn)行時(shí)導(dǎo)致 null 引用異常?!?的警告。值類型則沒有任何警告。
ok,再來看下這段代碼:
- Module Module1Module Module1
 - Sub Main()Sub Main()
 - Dim rnd As New Random(1000)
 - For i As Integer = 0 To 4
 - Dim b As Boolean
 - Console.WriteLine(b)
 - If rnd.NextDouble > 0.5 Then
 - b = True
 - End If
 - Next
 - End Sub
 - End Module
 - Module Module1
 - Sub Main()
 - Dim rnd As New Random(1)
 - For i As Integer = 0 To 9
 - Dim b As Boolean
 - Console.WriteLine(b)
 - If rnd.NextDouble < 0.5 Then
 - b = True
 - End If
 - Next
 - For i As Integer = 0 To 9
 - Dim rnd1 As Random
 - If rnd1 Is Nothing Then
 - Console.WriteLine("yes")
 - Else
 - Console.WriteLine("no")
 - End If
 - rnd1 = New Random
 - Next
 - End Sub
 - End Module
 
照說每次循環(huán)都重新定義并初始化該局部變量,期望的輸出值應(yīng)該一直都是false.但猜猜實(shí)際最終輸出結(jié)果是什么,false,false,false,true,true??!
就是這個(gè)怪誕的行為讓我困擾了很久。
ok,我們看看對應(yīng)的il代碼:
- .method public static void Main() cil managed
 - {
 - .custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
 - .entrypoint
 - .maxstack 2
 - .locals init (
 - [0] class [mscorlib]System.Random rnd,
 - [1] bool b,
 - [2] int32 i,
 - [3] bool VB$CG$t_bool$S0,
 - [4] int32 VB$CG$t_i4$S0,
 - [5] int32 num)
 - L_0000: nop
 - L_0001: ldc.i4 0x3e8
 - L_0006: newobj instance void [mscorlib]System.Random::.ctor(int32)
 - L_000b: stloc.0
 - L_000c: ldc.i4.0
 - L_000d: stloc.2
 - L_000e: ldloc.1
 - L_000f: call void [mscorlib]System.Console::WriteLine(bool)
 - L_0014: nop
 - L_0015: ldc.i4.1
 - L_0016: stloc.1
 - L_0017: nop
 - L_0018: ldloc.2
 - L_0019: ldc.i4.1
 - L_001a: add.ovf
 - L_001b: stloc.2
 - L_001c: ldloc.2
 - L_001d: ldc.i4.4
 - L_001e: stloc.s num
 - L_0020: ldloc.s num
 - L_0022: ble.s L_000e
 - L_0024: ldc.i4.0
 - L_0025: stloc.s VB$CG$t_i4$S0
 - L_0027: ldloc.3
 - L_0028: call void [mscorlib]System.Console::WriteLine(bool)
 - L_002d: nop
 - L_002e: ldc.i4.1
 - L_002f: stloc.3
 - L_0030: nop
 - L_0031: ldloc.s VB$CG$t_i4$S0
 - L_0033: ldc.i4.1
 - L_0034: add.ovf
 - L_0035: stloc.s VB$CG$t_i4$S0
 - L_0037: ldloc.s VB$CG$t_i4$S0
 - L_0039: ldc.i4.4
 - L_003a: stloc.s num
 - L_003c: ldloc.s num
 - L_003e: ble.s L_0027
 - L_0040: nop
 - L_0041: ret
 - }
 
暈……編譯器居然自動(dòng)把變量b聲明提升到循環(huán)體之外……因此就出現(xiàn)了上述的行為。(注:暈啊,以前學(xué)c的時(shí)候,就學(xué)過for(;;){int i=5;}里,i只在***次聲明,以前的基礎(chǔ)知識全部忘光光。感謝psic的指正。)
我個(gè)人猜測,編譯器這樣做的原因大概是為了性能??墒沁@樣實(shí)在是容易造成奇異的行為,最郁悶的是,值類型的變量,編譯器根本連警告都沒有。
所以,VB.NET使用局部變量,尤其在循環(huán)體內(nèi)使用局部變量,一定要進(jìn)行初始化。
【編輯推薦】















 
 
 
 
 
 
 