Section 1.9. The Compiler


1.9. The Compiler

Once you save your program to disk, you must compile the code to create your application. Compiling your source code means running a compiler and identifying the source code file. You run the compiler by opening a command prompt (DOS box) and entering the program name csc . Then you "pass in" your source code file by entering the filename on the command line, as in the following:

 csc HelloWorld.cs 

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 IL. When you run the program, the .NET Just In Time (JIT) compiler reads your IL code and produces an executable application in memory.

The IL code is actually stored in a .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.


Microsoft provides a command window with the correct environment variables set. Open the command window by selecting the following menu items in this order (your installation may vary slightly): Start Programs Microsoft Visual Studio 2005 Visual Studio Tools Visual Studio 2005 Command Prompt.

Navigate to the directory in which you created your code file and enter the following command:

 csc HelloWorld.cs 

The Microsoft C# compiler compiles your code; when you display the directory, you'll find the compiler has produced an executable file called helloworld.exe . Type helloworld at the command prompt, and your program executes, as shown in Figure 1-4.

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

Figure 1-4. Compiling and running Hello World

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.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net