对于条件VB.NET vs C#

C#:
static class Module1
{     
    public static void Main()
    {    
        for (index = 1; index <= GetCount(); index++) {
            Console.WriteLine("For {0}",index);
        }
        Console.ReadKey();
    }

    public static int GetCount()
    {
        Console.WriteLine("GetCount");
        return 10;
    }
}

结果(C#重新检查条件):

GetCount
For 1
GetCount
For 2
GetCount
For 3
GetCount
For 4
GetCount
For 5
GetCount
For 6
GetCount
For 7
GetCount
For 8
GetCount
For 9
GetCount
For 10
GetCount

VB.NET

Module Module1    
  Sub Main()
    For index = 1 To GetCount()
      Console.WriteLine("For {0}",index)
    Next
    Console.ReadKey()
  End Sub

  Public Function GetCount() As Integer
    Console.WriteLine("GetCount")
    Return 10
  End Function
End Module

结果(VB.NET不重新检查条件):

GetCount
For 1
For 2
For 3
For 4
For 5
For 6
For 7
For 8
For 9
For 10

a)为什么VB.NET不遵守每次重复的“条件”的“规则”?
b)有没有办法强制VB重新检查这种情况?

C#和VB.NET是不同的语言,类似的关键字可以有不同的语义.

the docs for For ... Next(我的重点):

When a For...Next loop starts,Visual Basic evaluates start,end,and step. This is the only time it evaluates these values.

从C#规范的第8.8.3节(同上):

When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement),the expressions of the for-iterator,if any,are evaluated in sequence,and then another iteration is performed,starting with evaluation of the for-condition in the step above.

如果你想强制每次检查条件,VB.NET提供极其灵活的Do …循环,它可以在循环开始或结束时运行While或Until条件.使用该语句,每个循环都会评估循环条件.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...