Here’s a cute little puzzler I got from the blog hackification — why the does code below compile?
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { https://www.globalnerdy.com System.Console.WriteLine("Hello from Global Nerdy!"); System.Console.WriteLine("(Press ENTER to continue)"); System.Console.ReadLine(); } } }
Here’s what the output of the program looks like:
Why does the program compile even though the first line of the Main()
method is a “bareword” URL? See if you can figure it out on your own rather than running it through the compiler – doing that gives away the answer.
I’ll post the answer in the comments.
6 replies on “Why Does This C# Code Compile?”
The Asnwer:
The compiler interprets the line
http://www.globalnerdy.com
this way:http:
is a label.//www.globalnerdy.com
is a comment.That was my first guess and I’ve never done anything with C#. Still a neat little puzzle though. =)
I guess the simplest answer is correct. I was thinking that maybe C# parses URLs in some way but the label-comment combo came to mind too.
Label? Does C# have GOTOs?
@David Janes: Yup, there’s a
goto
statement in C#.C# also has the
break
andcontinue
keywords, but unlike Java, they don’t support labels and are limited to breaking out of or starting at the next iteration of the current loop. That’s what C#’sgoto
(which java doesn’t have) is for.I first saw this in the Java Puzzlers book. Full of fun little compiler oddities.