Free mag vol1 | Page 108

CHAPTER 2  BUILDING C# APPLICATIONS Another nice by-product of working with csc.exe in the raw is that you become that much more comfortable manipulating other command-line tools included with the .NET Framework 4.5 SDK. As you will see throughout this book, a number of important utilities are accessible only from the command line (such as gacutil.exe, ngen.exe, ilasm.exe, and aspnet_regiis.exe). To illustrate how to build a .NET application IDE-free, we will build a simple executable assembly named TestApp.exe using the C# command-line compiler and Notepad. First, you need some source code. Open Notepad (using the Start  All Programs  Accessories menu option) and enter the following trivial C# class definition: // A simple C# application. using System; class TestApp { static void Main() { Console.WriteLine("Testing! 1, 2, 3"); } } After you have finished, save the file in a convenient location (e.g., C:\CscExample) as TestApp.cs. Now let’s get to know the core options of the C# compiler.  Note C# code files take a *.cs file extension. Unlike Java, the name of the file need not have any mapping to the name of the type (or types) it is defining. Specifying Input and Output Targets The first point of interest is to understand how to specify the name and type of assembly to create (e.g., a console application named MyShell.exe, a code library named MathLib.dll, a Windows Presentation Foundation application named Halo8.exe, and so forth). Each possibility is represented by a specific flag passed into csc.exe as a command-line parameter (see Table 2-1). Table 2-1. Common Output Options of the C# Compiler Option Meaning in Life /out This option is used to specify the name of the assembly to be created. By default, the assembly name is the same as the name of the initial input *.cs file. /target:exe This option builds an executable console application. This is the default assembly output type, and thus may be omitted when building this type of application. /target:library This option builds a single-file *.dll assembly. 41