13

During a demo I saw a piece of test code where the developer had pasted an url in the code. And when the developer build the application everything worked, but we where all very curious why the compiler accepted the url as a line.

public class Foo
{
   // Why doesn't 'http://www.foo.org' break the build?
    public void Bar()
    {
        http://www.foo.org
        Console.WriteLine("Do stuff");
    }
}

Why does the code above build? Does the compiler treat the line as a comment?

leppie
  • 115,091
  • 17
  • 196
  • 297
smoksnes
  • 10,509
  • 4
  • 49
  • 74
  • 4
    Cool, I'm going to do exactly this and have my code reviewed, to mess with my coworkers, thanks :) – C.Evenhuis May 13 '16 at 06:22
  • C# supports `goto`, here compiler reads `http:` as a label not as a URL – Raghuveer May 13 '16 at 06:23
  • You can clearly see whats going on here. But in IDE its not clearly explicit, because it's changing color to blue and link like. – SᴇM May 13 '16 at 06:34
  • @SeM , Yes. When I saw the accepted answer it was very obvious. But the IDE kinda disguised it by allowing click. Besides I haven't used alot of goto's since I left VB (except in switches). – smoksnes May 13 '16 at 06:40

1 Answers1

23

If you try the exact code above, you get warning CS0164: This label has not been referenced.

The warning here offers a clear hint as to what has happened.

Pasting the URL has created a label http:, e.g. for use with goto, immediately followed by a single-line comment, //www.foo.org.

yaakov
  • 5,552
  • 35
  • 48