EndPoint:C#中的语法 - 这是什么?

时间:2012-06-27 12:56:07

标签: c# .net windows winforms .net-3.5

我正在审核我最近加入的项目的一些代码,并且在 .NET 3.5 C#Win Forms应用程序中,我发现了这一点:

public void foo()
{
    //Normal code for function foo.

//This is at the end and it is left-indented just as I put it here.
EndPoint:
    {
    }
}

当我点击“ EndPoint / Go To Definition”时,它说“无法导航到端点”但整个项目非常小并且编译/运行时没有错误,所以它不是缺少参考或任何东西。

什么是EndPoint,这个语法的名称是什么:{}?

2 个答案:

答案 0 :(得分:5)

用于goto。请参阅:http://msdn.microsoft.com/en-us/library/13940fs2%28VS.71%29.aspx

带冒号的语法指定goto语句将控件转移到的标签。您可以在C#中使用它,但大多数开发人员都倾向于避免使用它。有时打破嵌套循环会很有用(这是我能为#34;合法"用法提出的最好的)

这里有一篇关于goto的一些更有用的用法的精彩文章:http://weblogs.asp.net/stevewellens/archive/2009/06/01/why-goto-still-exists-in-c.aspx

编辑:只是评论你得到的关于定义的错误,这是可以理解的。没有"定义"标签的来源。也许"去定义"在goto Endpoint;可能跳到标签,但我不确定 - 从未尝试过。如果您在那里的代码只有Endpoint:标签,但在任何地方都没有goto Endpoint;,那么删除标签应该是安全的,因为(我假设)它是未使用过的遗留旧代码。

答案 1 :(得分:2)

其他人已经解释了EndPoint:是什么。额外的括号正在创建一个新的范围。通过创建内部范围,您可以执行类似这样的操作

public Foo()
{
    {
        int bar = 10;
        Console.WriteLine(bar);
    }

    Console.WriteLine(bar); //Error: "Cannot resolve symbol bar."  It does not exist in this scope.

    {
        int bar = 20;  //Declare bar again because the first bar is out of scope.
        Console.Writeline(bar);
    }
}