Chapter 2 - C# Basics | |
bySimon Robinsonet al. | |
Wrox Press 2002 | |
So far, we have seen how to compile console applications using csc.exe , but what about other types of application? What if we want to reference a class library? We will look at all the options for csc.exe in C# Compilation Options, but for now we will look at the most important options.
To answer the first question, we can specify what type of file we want to create using the /target switch, often abbreviated to /t . This can be one of the following:
Option | Output |
---|---|
/t:exe | A console application (the default) |
/t:library | A class library with a manifest |
/t:module | A component without a manifest |
/t:winexe | A Windows application (without a console window) |
If we want a non-executable file (such as a DLL) to be loadable by the .NET runtime, we must compile it as a library. If we compile a C# file as a module, no assembly will be created. Although modules cannot be loaded by the runtime, they can be compiled into another manifest using the /addmodule switch. As we will see later in the book (in Chapter 17), this is particularly useful for including metadata held in AssemblyInfo.cs files in an assembly.
Another option we need to mention is /out . This allows us to specify the name of the output file produced by the compiler. If the /out option isn't specified, the compiler will base the name of the output file on the name of the input C# file, adding an extension according to the target type (for example, .exe for a Windows or console application, or .dll for a class library). Note that the /out and /t , or /target, options must precede the name of the file we want to compile.
If we want to reference types in assemblies that aren't referenced by default, we can use the /reference or /r switch, together with the path and filename of the assembly. The following example demonstrates how we can compile a class library, and then reference that library in another assembly. It consists of two files:
The class library
A console application, which will call a class in the library.
The first file is called MathLibrary.cs and contains the code for our DLL. To keep things simple, it contains just one (public) class, MathLib , with a single method that adds two int s:
namespace Wrox.ProCSharp.Basics { public class MathLib { public int Add(int x, int y) { return x + y; } } }
We can compile this C# file into a .NET DLL using the following command:
csc /t:library MathLibrary.cs
The console application, MathClient.cs , will simply instantiate this object, and call its Add() method, displaying the result in the console window:
using System; namespace Wrox.ProCSharp.Basics { class Client { public static void Main() { MathLib mathObj = new MathLib(); Console.WriteLine(mathObj.Add(7,8)); } } }
We can compile this using the /r switch to point at or reference our newly compiled DLL:
csc MathClient.cs /r:MathLibrary.dll
We can then run it as normal just by entering MathClient at the command prompt. This will display the number 15 the result of our addition.