CHAPTER 14 BUILDING AND CONFIGURING CLASS LIBRARIES
// Hexagon class
public class Hexagon { /* More interesting members... */ }
}
// Square class
public class Square { /* Even more interesting members... */ }
While the C# compiler has no problems with a single C# code file containing multiple types, this can
be cumbersome when you want to reuse class definitions in new projects. For example, say you are
building a new project and only need to use the Circle class. If all types are defined in a single code file,
you are more or less stuck with the entire set. Therefore, as an alternative, you can split a single
namespace across multiple C# files. To ensure each type is packaged into the same logical group, simply
wrap the given class definitions in the same namespace scope, like so:
// Circle.cs
using System;
namespace MyShapes
{
// Circle class
public class Circle { /* Interesting methods... */ }
}
// Hexagon.cs
using System;
namespace MyShapes
{
// Hexagon class
public class Hexagon { /* More interesting methods... */ }
}
// Square.cs
using System;
namespace MyShapes
{
// Square class
public class Square { /* Even more interesting methods... */ }
}
In both cases, notice how the MyShapes namespace acts as the conceptual “container” of these
classes. When another namespace (such as CustomNamespaces) wishes to use types in a separate
namespace, you make use of the using keyword, just as you would when using namespaces of the .NET
base class libraries, as follows:
// Bring in a namespace from the base class libraries.
using System;
// Make use of types defined the MyShapes namespace.
using MyShapes;
504