The Compiler

After you write your program in an editor and save it to disk, you must compile it. Compiling is the process of turning the code that you can read into code that the machine can read. For that, you need a compiler. Then, once you’ve compiled the program, you need to run and test it.

The job of the compiler is to turn your source code into a working program. It turns out to be just slightly more complicated than that because .NET uses an intermediate language called Microsoft Intermediate Language (MSIL, sometimes abbreviated as IL). The compiler reads your source code and produces MSIL. When you run the program, the .NET Just In Time (JIT) compiler reads your MSIL code and produces an executable application in memory. You won’t see any of this happen, but it’s a good idea to know what’s going on behind the scenes.

Tip

The MSIL code is actually stored in an .exe file, but this file does not contain executable code. It contains the information needed by the JIT to execute the code when you run it.

Visual Studio provides a built-in compiler that you’ll use pretty much all the time. To compile and run Hello World, select Debug → Start Without Debugging, and your program executes, as shown in Figure 1-5. You can also press Ctrl-F5 to do the same thing. You may notice a button on the toolbar () that will also compile and run your program, but you don’t want to use that this time. If you do (and feel free to try this), your program will still execute, but the console window will close immediately, before you have a chance to see what you’ve done. Start Without Debugging opens the window, but adds the line “Press any key to continue … " after your program’s output. Go ahead and press a key now to dismiss the window and end the program.

Presto! You are a C# programmer. That’s it, close the book, you’ve done it. OK, don’t close the book—there are details to examine, but take a moment to congratulate yourself. Have a cookie.

Granted, the program you created is one of the simplest C# programs imaginable, but it is a complete C# program, and it can be used to examine many of the elements common to C# programs.

If your program didn’t run as anticipated, don’t panic. If something is wrong in the code, Visual Studio will pop up a dialog box saying “There were build errors. Would you like to continue and run the last successful build?” In a program this simple, you most likely made what’s called a syntax error, which is a term programmers use because they don’t want to admit they made a typo, which is usually what happened. Select No in this dialog and Visual Studio will open an error window at the bottom of the interface with a message that may or may not be helpful, depending on exactly what’s wrong.

These are the results you’ll see in the command window after you’ve compiled and run Hello World.

Figure 1-5. These are the results you’ll see in the command window after you’ve compiled and run Hello World.

Go back and check your code very carefully, and make sure it matches the code in Example 1-1 exactly. Make sure there’s a semicolon at the end of the line containing the WriteLine, and that you’ve capitalized correctly. Make sure you have open and close quotation marks around "Hello World” and make sure you have open and close parentheses around the quotes. Make sure the first line you added starts with two forward slashes (//); the entire line should appear in green, if you’ve done it correctly. Make any necessary fixes, and then try to build and run the program again.

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.