Free mag vol1 | Page 563

CHAPTER 14  BUILDING AND CONFIGURING CLASS LIBRARIES namespace CustomNamespaces { public class Program { static void Main(string[] args) { Hexagon h = new Hexagon(); Circle c = new Circle(); Square s = new Square(); } } } For this particular example, the assumption is that the C# file(s) that define the MyShapes namespace are part of the same Console Application project that contains the file defining the CustomNamespaces namespace; in other words, all of the files are used to compile a single .NET executable assembly. If you defined the MyShapes namespace within an external assembly, you would also need to set a reference to that library before you could compile successfully. You’ll learn all the details of building applications that make use of external libraries during the course of this chapter. Resolving Name Clashes with Fully Qualified Names Technically speaking, you are not required to use the C# using keyword when referring to types defined in external namespaces. You could use the fully qualified name of the type, which, as you may recall from Chapter 1, is the type’s name prefixed with the defining namespace. For example: // Note we are not importing MyShapes anymore! using System; namespace CustomNamespaces { public class Program { static void Main(string[] args) { MyShapes.Hexagon h = new MyShapes.Hexagon(); MyShapes.Circle c = new MyShapes.Circle(); MyShapes.Square s = new MyShapes.Square(); } } } Typically, there is no need to use a fully qualified name. Not only does it require a greater number of keystrokes, it also makes no difference whatsoever in terms of code size or execution speed. In fact, in CIL code, types are always defined with the fully qualified name. In this light, the C# using keyword is simply a typing time saver. However, fully qualified names can be very helpful (and sometimes necessary) to avoid potential name clashes when using multiple namespaces that contain identically named types. Assume you have a new namespace termed My3DShapes, which defines the following three classes, capable of rendering a shape in stunning 3D: // Another shape-centric namespace. using System; 505