C Sharp

Using the Command-Line Compiler

If you're using an editor with a built-in feature for building C# applications, this step might be awkward. However, to stay as editor-agnostic as possible, I'm going to use the C# command-line compiler (csc.exe) . This provides two benefits. First, it means that no matter what environment you're using, the steps for building the demos will always work. Second, learning the different compiler options will help you in the long run in situations where your editor doesn't provide you complete control over this step.

Once you've typed in the "Hello, World" code, open a command window and navigate to the folder where the HelloWorld.cs file is. Now type in the following: -

csc helloWorld.cs

If everything works correctly, you should see results similar to those shown in Figure 3-2, where the compiler name and version are displayed along with any errors or warnings. As you can see, I'm using a beta version of the compiler at the time of this writing. Despite the version of the compiler you're using, you should not see any errors or warnings as a result of this simple example.


Figure 3-2 Compiling HelloWorld.cs should result in no errors or warnings.-

If you receive an error along the lines of 'csc' is not recognized as an internal or external command, operable program or batch file, it probably means that you haven't installed the .NET SDK, which includes the C# compiler.

If you invoke the C# compiler incorrectly-such as passing it an invalid file name-or you call it with the /? argument, the compiler will list every possible switch that can be set when compiling your applications. I don't want to bog down this discussion with descriptions of all the flags that can be set for the compiler, so instead I'll focus on the switches salient to the current discussion, and you can investigate the rest at your leisure.