Free mag vol1 | Page 110

CHAPTER 2  BUILDING C# APPLICATIONS Referencing External Assemblies Next, let’s examine how to compile an application that makes use of types defined in a separate .NET assembly. And just in case you are wondering how the C# compiler understood your reference to the System.Console type, recall from Chapter 1 that mscorlib.dll is automatically referenced during the compilation process (if for some strange reason you wish to disable this feature, you may specify the /nostdlib option of csc.exe). Let’s update the TestApp application to display a Windows Forms message box. Open your TestApp.cs file and modify it as follows: using System; // Add this! using System.Windows.Forms; class TestApp { static void Main() { Console.WriteLine("Testing! 1, 2, 3"); } // Add this! MessageBox.Show("Hello..."); } Notice you are importing the System.Windows.Forms namespace via the C# using keyword (introduced in Chapter 1). Recall that when you explicitly list the namespaces used within a given *.cs file, you avoid the need to make use of fully qualified names of a type (which can lead to hand cramps). At the command line, you must inform csc.exe which assembly contains the namespaces you are using. Given that you have made use of the System.Windows.Forms.MessageBox class, you must specify the System.Windows.Forms.dll assembly using the /reference flag (which can be abbreviated to /r). csc /r:System.Windows.Forms.dll TestApp.cs If you now rerun your application, you should see a message box appear (see Figure 2-3) in addition to the console output. Figure 2-3. Your first graphical user interface application 43