Nested Namespaces


Apart from writing namespaces beside each other, as demonstrated in Listing 15.2, you can also nest namespaces inside other namespaces as mentioned earlier. The following scenario provides an example of a typical situation where namespace nesting would be convenient.

Suppose the editors at Sams Publishing wanted to provide a convenient way for programmers to reuse the code written in several of their C# books, including this one. An overall namespace hierarchy for doing this could look like that shown in Listing 15.3. An outermost namespace called Sams contains namespaces beside each other, named after each of Sams C# books (for brevity sake, only the two namespaces CSharpPrimerPlus and SamsTeachYourselfCSharpIn21Days have been shown). Each book namespace again contains namespaces beside each other with different projects of the book, such as the ElevatorSimulation and BankSimulation programs contained in this book.

Listing 15.3 Providing Convenient Access to Code from Sams C# Books
01: namespace Sams 02: { 03:     namespace CSharpPrimerPlus 04:     { 05:         namespace ElevatorSimulation 06:         { 07:             class Elevator 08:             { 09:                 private AllRounder mrUseful; 10:                 ... 20:             } 21: 22:             class Building 23:             {                     ... 38:             }                 <Other_Sams.CSharpPrimerPlus.ElevatorSimulation                _ classes_and_types> 40          } 45:         namespace BankSimulation 46:         { 47:             class Building 48:             {                     ... 57:             }                 <Other Sams.CSharpPrimerPlus.BankSimulation classes_and_types> 60:         }             <Other_namespaces nested inside the Sams.CSharpPrimerPlus              namespace> 70:     } 75:     namespace SamsTeachYourselfCSharpIn21Days 76:     {             <Sams.SamsTeachYourselfCSharpIn21Days_namespace with namespaces similar to  graphics/ccc.gifElevatorSimulation and BankSimulation> 85:     }         <Other_Sams C Sharp Book namespaces positioned here> 90:     class AllRounder 91:     {             ... 98:     } 99: } 

The following points are important in relation to Listing 15.3:

  • As per convention, you should use your company or product name for the outermost namespace, such as Sams in Listing 15.3. Not only does this effectively prevent name clashes between classes located in namespaces written by different companies, it also gives you an idea of who wrote the code you are reusing (and lets you get a bit of free advertising when other programmers use your own namespaces).



    The .NET SDK documentation contains more guidelines about how to name and define namespaces.

  • A namespace name is made up of the names of the namespaces within which it resides and its own short name (such as ElevatorSimulation in line 5). The top-level namespace name is the first part of the name, and its own short name is the last part. Each part is separated from the other parts with periods. So the Elevator class in lines 7 20 of Listing 15.3 is written inside a namespace called Sams.CSharpPrimerPlus.ElevatorSimulation, and its own proper long name is Sams.CSharpPrimerPlus.ElevatorSimulation.Elevator.

  • You can use the same syntax as specified in the previous point to define a namespace instead of using the intermittently cumbersome syntax shown in Listing 15.3, which uses curly braces and indenting. I will demonstrate this syntax in the next point.

  • If you define the same namespace in several different parts of the compilation unit (and even in different compilation units, as you will see later) each of these namespace definitions are in effect contributing its code to this same namespace. For example, if, apart from the Elevator and Building classes, you wanted to add a class called Person to the Sams.CSharpPrimerPlus.ElevatorSimulation namespace in Listing 15.3, you could do so by using the syntax suggested in the previous point. Write the following lines after the last closing brace (in line 99) of Listing 15.3.

     Sams.CSharpPrimerPlus.ElevatorSimulation {     class Person     {         ...     } } 

    The Sams.CSharpPrimerPlus.ElevatorSimulation namespace would now contain the Elevator, Building, and Person classes exactly as if the Person class definition had been inserted in between the Elevator and Building class definitions (after line 20) in the Sams.CSharpPrimerPlus.ElevatorSimulation namespace.

  • Notice that you cannot nest the longer qualified namespace names inside other namespaces when you define namespace parts. For example, you could not have inserted the Sams.CSharpPrimerPlus.ElevatorSimulation namespace part containing the Person class definition anywhere inside the Sams namespace.

  • When we involve assemblies a little later in this namespace discussion, you will see how you can contribute code (in the form of namespace parts) to the same namespace, even from different compilation units.

  • The code you write in an inner namespace can refer to a class defined in one of this namespace's outer namespaces by using this class's short name. For example, the Sams.CSharpPrimerPlus.ElevatorSimulation.Elevator class (lines 7 20) can, as illustrated, simply specify the Sams.AllRounder class (in line 9) by writing the short name AllRounder. However, the contrary is not true. If you wanted to specify the Sams.CSharpPrimerPlus.ElevatorSimulation.Elevator class in the Sams.AllRounder class, you would have to specify this class by including the namespace names that would bridge the Sams namespace to this inner namespace. In this case, the specification inside the Sams.AllRounder class would look like the following:


    Notice that we don't need to include Sams as in:


    because AllRounder exists inside Sams.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata © 2008-2017.
If you may any questions please contact us: