Categories
Uncategorized

Why Does This C# Code Compile?

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:

Console output: "Hello from Global Nerdy! (Press ENTER to continue)"

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?”

@David Janes: Yup, there’s a goto statement in C#.

C# also has the break and continue 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#’s goto (which java doesn’t have) is for.

Comments are closed.