CHAPTER 2 BUILDING C# APPLICATIONS 37 Figure (Web host server)
CHAPTER 2 BUILDING C# APPLICATIONS 37 Figure 2-1. TestApp in action TestApp.exe can now be run from the command line (see Figure 2-1). Referencing External Assemblies Next up, let s examine how to compile an application that makes use of types defined in a separate .NET assembly. Speaking of which, 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 behavior, you may specify the /nostdlib flag). To illustrate the process of referencing external assemblies, let s update the TestApp application to display aWindows Forms message box. Open your TestApp.cs file and modify it as follows: using System; // Add this! using System.Windows.Forms; class TestApp { public static void Main() { Console.WriteLine(”Testing! 1, 2, 3″); // Add this! MessageBox.Show(”Hello…”); } } Notice the reference to 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 (which can lead to hand cramps). At the command line, you must inform csc.exe which assembly contains the used namespaces. Given that you have made use of the 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 what appears in Figure 2-2 in addition to the console output.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.