他们将在性能方面有效地生成相同的代码。
从可维护性和调试的角度来看,选项 #1 更受欢迎,因为它允许通过 Visual Studio 更轻松地注入断点,并且通常更容易理解,因为每行中的逻辑量更小。
我实际上主张在这两个选项之间建立一个快乐的媒介,称之为选项#1.5:
Dim html As String = WebBrowser.DocumentText
Dim htmlString As String = "size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">"
Dim dIndex As Integer = html.IndexOf(htmlString)
If (dIndex > -1) Then
Dim lIndex As Integer = sDomain.IndexOf("<")
Dim sDomain As String = html.Substring(dIndex + 26, 20)
LblSubDomain.Text = sDomain.Substring(0, lIndex)
Else
LblSubDomain.Text = "Cannot Find Sub Domain Extension"
End If
这样可以减少总代码行数,但保留了选项 #1 提供的大部分可读性、可维护性和可调试性。
下面是通过 Reflector 反编译成中间语言 (IL) 的代码:
注意:MySub()
选项#1 是否被反编译为 IL。
.method public instance void MySub() cil managed
{
.maxstack 4
.locals init (
[0] int32 num,
[1] string str,
[2] string str2,
[3] int32 num2,
[4] string str3,
[5] bool flag)
L_0000: nop
L_0001: ldarg.0
L_0002: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_0007: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_000c: stloc.1
L_000d: ldstr "size=\"15\" maxlength=\"40\">"
L_0012: stloc.2
L_0013: ldloc.1
L_0014: ldloc.2
L_0015: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_001a: stloc.0
L_001b: ldloc.0
L_001c: ldc.i4.m1
L_001d: cgt
L_001f: stloc.s flag
L_0021: ldloc.s flag
L_0023: brfalse.s L_0057
L_0025: ldloc.1
L_0026: ldloc.0
L_0027: ldc.i4.s 0x1a
L_0029: add.ovf
L_002a: ldc.i4.s 20
L_002c: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
L_0031: stloc.s str3
L_0033: ldloc.s str3
L_0035: ldstr "<"
L_003a: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_003f: stloc.3
L_0040: ldarg.0
L_0041: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
L_0046: ldloc.s str3
L_0048: ldc.i4.0
L_0049: ldloc.3
L_004a: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
L_004f: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
L_0054: nop
L_0055: br.s L_0069
L_0057: nop
L_0058: ldarg.0
L_0059: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
L_005e: ldstr "Cannot Find Sub Domain Extension"
L_0063: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
L_0068: nop
L_0069: nop
L_006a: nop
L_006b: ret
}
注意:MySub2()
选项#2 是否被反编译为 IL。
.method public instance void MySub2() cil managed
{
.maxstack 6
.locals init (
[0] bool flag)
L_0000: nop
L_0001: ldarg.0
L_0002: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_0007: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_000c: ldstr "size=\"15\" maxlength=\"40\">"
L_0011: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_0016: ldc.i4.m1
L_0017: cgt
L_0019: stloc.0
L_001a: ldloc.0
L_001b: brfalse.s L_008f
L_001d: ldarg.0
L_001e: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
L_0023: ldarg.0
L_0024: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_0029: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_002e: ldarg.0
L_002f: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_0034: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_0039: ldstr "size=\"15\" maxlength=\"40\">"
L_003e: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_0043: ldc.i4.s 0x1a
L_0045: add.ovf
L_0046: ldc.i4.s 20
L_0048: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
L_004d: ldc.i4.0
L_004e: ldarg.0
L_004f: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_0054: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_0059: ldarg.0
L_005a: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
L_005f: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
L_0064: ldstr "size=\"15\" maxlength=\"40\">"
L_0069: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_006e: ldc.i4.s 0x1a
L_0070: add.ovf
L_0071: ldc.i4.s 20
L_0073: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
L_0078: ldstr "<"
L_007d: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
L_0082: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
L_0087: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
L_008c: nop
L_008d: br.s L_00a1
L_008f: nop
L_0090: ldarg.0
L_0091: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
L_0096: ldstr "Cannot Find Sub Domain Extension"
L_009b: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
L_00a0: nop
L_00a1: nop
L_00a2: nop
L_00a3: ret
}
注意:Reflector在试用期后不再是免费产品,但是有免费的替代品可以为您的代码获取 IL(ildasm
是 .NET Framework 中内置的工具,ILSpy是对 Reflector 不再免费的开源响应)