您可以使用 while(condition)
在VB.NET中实现相同的效果。
这就是与编译器的区别。 VB.NET编译器只是表现得不同。
如果您在VB.NET上使用反射器,您会看到这个C#反射代码:
[STAThread]
public static void Main()
{
int VB$t_i4$L0 = GetCount();
for (int index = 1; index <= VB$t_i4$L0; index++)
{
Console.WriteLine("For {0}", index);
}
Console.ReadKey();
}
以下是IL代码(请注意IL_002):
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 47 (0x2f)
.maxstack 2
.locals init ([0] int32 index,
[1] int32 VB$t_i4$L0,
[2] int32 VB$CG$t_i4$S0)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: call int32 ConsoleApplication2.Module1::GetCount()
IL_0007: stloc.1
IL_0008: stloc.0
IL_0009: br.s IL_0021
IL_000b: ldstr "For {0}"
IL_0010: ldloc.0
IL_0011: box [mscorlib]System.Int32
IL_0016: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_001b: nop
IL_001c: nop
IL_001d: ldloc.0
IL_001e: ldc.i4.1
IL_001f: add.ovf
IL_0020: stloc.0
IL_0021: ldloc.0
IL_0022: ldloc.1
IL_0023: stloc.2
IL_0024: ldloc.2
IL_0025: ble.s IL_000b
IL_0027: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
IL_002c: pop
IL_002d: nop
IL_002e: ret
} // end of method Module1::Main
对于与C#相关的代码,情况有所不同(检查在循环内部):
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 50 (0x32)
.maxstack 2
.locals init ([0] int32 index,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: br.s IL_001c
IL_0005: nop
IL_0006: ldstr "For {0}"
IL_000b: ldloc.0
IL_000c: box [mscorlib]System.Int32
IL_0011: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0016: nop
IL_0017: nop
IL_0018: ldloc.0
IL_0019: ldc.i4.1
IL_001a: add
IL_001b: stloc.0
IL_001c: ldloc.0
IL_001d: call int32 Module1::GetCount()
IL_0022: cgt
IL_0024: ldc.i4.0
IL_0025: ceq
IL_0027: stloc.1
IL_0028: ldloc.1
IL_0029: brtrue.s IL_0005
IL_002b: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
IL_0030: pop
IL_0031: ret
} // end of method Module1::Main