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

我正在审查最近join的项目中的一些代码,并在.NET 3.5C#Win Forms Application中find了这个:

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”时,它会显示“Can not Navigate to Endpoint”,但整个项目非常小,编译/运行没有错误,所以它不是缺less的引用或任何东西。

EndPoint是什么?这个名字是什么?{}?

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

带有冒号的语法指定goto语句将控制转移到的标签。 您可以在C#中使用它,但大多数开发人员往往会避免它。 有时候,打破嵌套循环可能是有用的(这是最好的,我可以拿出一个“合法的”用法)

下面是一些更有用的goto用法: http : //weblogs.asp.net/stevewellens/archive/2009/06/01/why-goto-still-exists-in-c.aspx

编辑:只是要评论你所定义的错误,这是可以理解的。 标签没有“定义”来源。 也许在goto Endpoint;上“定义” 可能跳转到标签,但我不知道 – 从来没有尝试过。 如果您在那里的代码只有Endpoint:标签但没有goto Endpoint; 任何地方,那么删除标签应该是安全的,因为(我假设)这是一个未使用的旧代码的残余。

其他人已经解释了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); } }