Programming in the Key of C#

Compiling C# Programs on the Command Line

If you're familiar with using the Command Prompt window under Windows, you can compile and run your C# programs on the command line. You may want to do this to get a better feel for what the compiler does in isolation from integrated development environments (IDEs) such as Visual C# .NET and Charles Petzold's Key of C# program. Many programmers compiled many programs on the command line for many years before IDEs became widely available, and it's still a legitimate option.

Please don't try to use the Command Prompt to compile your C# programs if you're not familiar with basic MS-DOS commands such as DIR, MD, CD, or COPY. Learn how to use MS-DOS commands first.

To use the Microsoft C# compiler on the command line, you must have installed the .NET Framework Software Development Kit (SDK). The .NET Framework SDK is automatically installed if you've installed Visual C# .NET or Visual Studio .NET.

The C# Compiler

The command line C# compiler is a program named CSC.EXE. This program makes use of a dynamic link library named CSCOMP.DLL that contains the actual compiler code. Another DLL, CSCOMPMGD.DLL ("C# compiler managed") is also included with the .NET Framework SDK and is accessible to .NET programs. See the documentation of the Microsoft.CSharp namespace for details. The Key of C# program makes use of the C# compiler in this namespace. The interface to CSCOMP.DLL, on the other hand, is not documented.

To run the C# compiler on the command line, the PATH environment variable must be set to indicate the directory in which CSC.EXE is stored. Visual C# .NET and Visual Studio .NET create special Command Prompt windows that are appropriate for running the C# compiler.

If you've installed Visual C# .NET or Visual Studio .NET, you can open an appropriate Command Prompt window by choosing Microsoft Visual Studio .NET on the Start program menu, then Visual Studio .NET Tools, and then Visual Studio .NET Command Prompt.

If instead you've installed the .NET Framework SDK, just open the regular Command Prompt window form the Accessories submenu of the Start program menu.

Once the Command Prompt window is open, you can check that you have access to the C# compiler by entering the command:

The C# compiler should display a list of compile options.

Edit, Compile, Run

Chapter 4 of Programming in the Key of C# describes how to edit, compile, and run a program named MyVeryFirstProgram.cs. If you're working on the command line, you'll probably first want to create a directory for this program: You then change to this directory: MS-DOS commands, directory names, and filenames are not case sensitive. You can type entirely in uppercase or lowercase if you want.

You can use any text editor to create the program. Under Windows, Notepad is convenient:

Because MyVeryFirstProgram.cs does not yet exist, Notepad will ask you if you want to create it. You can then type in the program, and select Save from the File menu (or press Ctrl-S) to save it to disk.

Back on the command line, you type

to compile the program. If no errors are reported, a file named MyVeryFirstProgram.exe should now exist in the directory. (Use the DIR command to find out.) You can then run the program like so: Because this program does not display any output, the MS-DOS command prompt will immediately return. Later programs in the book display output in the Command Prompt window before the prompt returns.

You may need to change MyVeryFirstProgram.cs or add something to it. You don't need to run and exit Notepad every time you want to edit the program. You can keep Notepad running loaded with the program you're working on, and just save the file whenever you want to compile it again.

Compiler Options

Chapter 8 of Programming in the Key of C# shows how to use a compiler option, specifically, the compiler option that causes run-time checking of integer overloads.

The compiler option to enable overflow checks is /checked. To enable overflow checking for the HowHigh program, compile using:

or To compile without integer overflow checking: or The C# compiler allows compiler options to be specified using a forward slash or dash. They can usually either precede the name of the C# source code file or follow it.

Multiple Source Code Files

Chapter 17 of Programming in the Key of C# involves a program that is divided into two source code files, specifically the files CalculationWithSeparateFile.cs and HandyCalcs.cs. To compile these source code files together into one program, specify both files on the command line: or with the files in the reverse order: In either case, the executable file will be named CalculationWithSeparateFile.exe because that's the file that contains the Main method.

Sometimes you'll be developing a program where two or more different classes have Main methods. In such a case you must inform the compiler which class contains the Main method that you want to be the program entry point. Use the compiler option /main or /m followed by a colon and the name of the class that contains the entry point to the program. The name of this class is case senstive!

Chapter 36 in Programming in the Key of C#has a program named Inheritance that involves a file named Inheritance.cs but also requires the Date.cs file from Chapter 35. Compiling these two files together into one program simply requires that you specify the correct filenames. Asssuming that you have a directory structure based on chapters and projects (similar to the Companion Content of the book downloadable from the Programming in the Key of C# page on the Microsoft Press site), the compile command would be:

When specifying relative filenames, the double dots indicate a parent directory.

Making and Using DLLs

Chapter 39 of Programming in the Key of C# describes how to make a Dynamic Link Library (DLL). To make a DLL rather than an EXE, you use the compiler option /target:library or /t:library. Here's the command to create Temperature.dll: The target option must appear before the filenames. The DLL is given a name based on the first filename. You can alternatively specify the output name you want using the /out compile option. This option, too, must appear before the filenames: The next program in Chapter 39 (TemperatureDemo) uses the Temperature.dll file. The Temperature.dll file is required by the compiler when it compile the TemperatureDemo program, and is also required by the TemperatureDemo.exe file when it runs. The easiest way to satisfy these requirements is to copy the Temperature.dll file into the same directory as TemperatureDemo.cs: When you compile TemperatureDemo.cs you must specify Temperature.dll as a reference: You can use /r instead of /reference. Multiple references must be separated with semicolons.

In Chapter 40, compiling the Clock program requires access to the System.dll file, where classes in the System.Timers namespace are stored. You compiler the Clock program like this:

You don't need to copy System.dll into the directory because it's located in a system directory (/Windows/Microsoft.NET/Framework/ followed by the version number of the .NET Framework that you're using.

BackHome

© Charles Petzold, 2003
cp@charlespetzold.com
This page last updated August, 2003