I'm reviewing some code on the project I recently joined, and in a C# Win Forms Application for .NET 3.5 I found this:
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:
{
}
}
When I click "EndPoint/Go To Definition" it says "Cannot Navigate to Endpoint" but the project as a whole is pretty small and compiles/runs without error, so it's not a missing reference or anything.
What is EndPoint and what is this syntax with the name : {}?
Others have explained what the EndPoint:
is. The extra braces are creating a new scope. By creating an inner scope you can do something like this
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);
}
}
Its for goto
. See: http://msdn.microsoft.com/en-us/library/13940fs2%28VS.71%29.aspx
The syntax with the colons specifies the labels where the goto
statement will transfer control to. You can use it in C#, but most developers tend to avoid it. Sometimes it can be useful to break out of nested loops (that's the best I can come up with for a "legitimate" usage)
Here's a nice writeup on some of the more useful usages of goto
: http://weblogs.asp.net/stevewellens/archive/2009/06/01/why-goto-still-exists-in-c.aspx
EDIT: Just to comment on the error you get about going to definition, that's understandable. There is no "definition" source for the label. Perhaps "go to definition" on the goto Endpoint;
might jump to the label, but I'm not sure -- never tried it. If your code that you have there only has the Endpoint:
label but no goto Endpoint;
anywhere, then it should be safe to delete the label because (I'm assuming) it's an unused remnant of old code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With