EndPoint: Syntax in C# - what is this?
Asked Answered
T

2

5

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 : {}?

Tootsy answered 27/6, 2012 at 12:56 Comment(9)
kill the original developer that wrote it, it's a goto!Kiss
It's a label! What's that doing in your Win Forms project?Tieback
Holy crap :p I haven't seen one of those in years even in C++. I didn't even know they kept that feature in C# :pTootsy
Kill Microsoft also for creating a feature that should be never used.Handyman
goto can actually be useful but there needs to be strong cause to use it. The braces are not required for a goto-statement and, I assume, were just placed there for readability.Smarm
Naming a go-to label EndPoint seems like a bad idea to me :p that was very misleading. :(Tootsy
The {} are there to prevent a compiler error. If your remove them, the next statement is }, and the C# compiler doesn't like that. It spit out the following errors in my test class: Only assignment, call, increment, decrement, and new object expressions can be used as a statement, Invalid expression term '}', and ; expected. (Note, the code compiled fine until I removed the {} after the label).Biblical
Thanks for the extra research effort @ Jon Senchyna :)Tootsy
never thought i seem it in C# but quite often when I was in vb6/C++ world...Gunshy
V
5

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.

Vatican answered 27/6, 2012 at 12:56 Comment(2)
Well it's not a "case" statement; it's not the same as switch/case if that's what you're thinking.Vatican
Touche, I meant goto label :) My heads everywhere today.Tootsy
R
2

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);
    }
}
Rhineland answered 27/6, 2012 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.