There are at least two ways to enter, compile, and run the programs in this book: use the Visual Studio .NET Integrated Development Environment (IDE), or use a text editor and a command-line compiler (along with some additional command-line tools to be introduced later).
Although you can develop software outside Visual Studio .NET, the IDE provides enormous advantages. These include indentation support, Intellisense word completion, color coding, and integration with the help files. Most important, the IDE includes a powerful debugger and a wealth of other tools.
Although this book tacitly assumes that you'll be using Visual Studio .NET, the tutorials focus more on the language and the platform than on the tools. You can copy all the examples into a text editor such as Windows Notepad or Emacs, save them as text files, and compile them with the C# command-line compiler that is distributed with the .NET Framework SDK. Note that some examples in later chapters use Visual Studio .NET tools for creating Windows Forms and Web Forms, but even these you can write by hand in Notepad if you are determined to do things the hard way.
To create the "Hello World" program in the IDE, select Visual Studio .NET from your Start menu or a desktop icon, and then choose File->New->Project from the menu toolbar. This will invoke the New Project window. (If you are using Visual Studio for the first time, the New Project window might appear without further prompting.) Figure 2-1 shows the New Project window.
To open your application, select Visual C# Projects in the Project Type window and select Console Application in the Templates window. You can now enter a name for the project and select a directory in which to store your files. Click OK, and a new window will appear in which you can enter the code in Example 2-1, as shown in Figure 2-2.
Notice that Visual Studio .NET creates a namespace based on the project name you've provided (HelloWorld), and adds a using System statement because nearly every program you write will need types from the System namespace.
Visual Studio .NET creates a class named Class1, which you are free to rename. When you rename the class, be sure to rename the file as well (Class1.cs). To reproduce Example 2-1, for instance, change the name of Class1 to HelloWorld, and rename the Class1.cs file (listed in the Solution Explorer window) to hello.cs.
Finally, Visual Studio .NET creates a program skeleton, complete with a TODO comment to get you started. To reproduce Example 2-1, remove the arguments (string args) and comments from the Main( ) method. Then copy the following two lines into the body of Main( ):
// Use the system console object System.Console.WriteLine("Hello World");
If you are not using Visual Studio .NET, open Notepad, type in the code from Example 2-1, and save the file as a text file named hello.cs.
There are many ways to compile and run the "Hello World" program from within Visual Studio .NET. Typically you can accomplish every task by choosing commands from the Visual Studio .NET menu toolbar, by using buttons, and, in many cases, by using key-combination shortcuts.
For example, to compile the "Hello World" program, press Ctrl-Shift-B or choose Build->Build Solution. As an alternative, you can click the Build button on the Build button bar (you may need to right-click on the toolbar to add the Build button bar). The Build button icon is shown in Figure 2-3.
To run the "Hello World" program without the debugger, you can press Ctrl-F5 on your keyboard, choose Debug->Start Without Debugging from the IDE menu toolbar, or press the Start Without Debugging button on the IDE Build toolbar, as shown in Figure 2-4 (you may need to customize your toolbar to make this button available). You can run the program without first explicitly building it; depending on how your options are set (Tools->Options), the IDE will save the file, build it, and run it, possibly asking you for permission at each step.
Use the following steps to compile and run the "Hello World" program using the C# command-line compiler:
Save Example 2-1 as the file hello.cs.
Open a command window (Start->Run and type in cmd).
From the command line, enter:
csc /debug hello.cs
This step will build the executable (EXE) file. If the program contains errors, the compiler will report them in the command window. The /debug command-line switch inserts symbols in the code so you can run the EXE under a debugger or see line numbers in stack traces. (You'll get a stack trace if your program generates an error that you do not handle.)
To run the program, enter:
You should see the venerable words "Hello World" appear in your command window.
Just In Time Compilation
Compiling hello.cs using csc creates an executable (EXE) file. Keep in mind, however, that the .exe file contains op-codes written in Microsoft Intermediate Language (MSIL), which is introduced in Chapter 1.
Interestingly, if you had written this application in VB.NET or any other language compliant with the .NET Common Language Specification, you would have compiled it into the same MSIL. By design, Intermediate Language (IL) code created from different languages is virtually indistinguishable; this is the point of having a common language specification in the first place.
In addition to producing the IL code (which is similar in spirit to Java's byte-code), the compiler creates a read-only segment of the .exe file in which it inserts a standard Win32 executable header. The compiler designates an entry point within the read-only segment; the operating system loader jumps to that entry point when you run the program, just as it would for any Windows program.
The operating system cannot execute the IL code, however, and that entry point does nothing but jump to the .NET Just In Time (JIT) compiler (also introduced in Chapter 1). The JIT produces native CPU instructions, as you might find in a normal .exe. The key feature of a JIT compiler, however, is that functions are compiled only as they are used, Just In Time for execution.