Namespaces and Compilation Units


Listing 15.2 shows the most important code fragments for defining two namespaces called ElevatorSimulation (spanning lines 3 61 and containing the Elevator, Person, and Building classes) and BankSimulation (in lines 63 101 and containing the Account, Bank, and Building classes). The two namespaces are positioned beside each other in the same compilation unit. The class MyGlobalNamespaceClass (lines 103 120) has been thrown in to demonstrate a class that exists beside two namespaces and outside any namespace. The latter fact, as discussed in the previous section, causes it to belong to the global nameless namespace.

Listing 15.2 Two Namespaces Written Beside Each Other in a Compilation Unit
001: using System; 002: 003: namespace ElevatorSimulation 004: { 005:     class Elevator 006:     {              ... 010:         private Person passenger;              ... 020:     }          ... 030:     class Person 031:     {              ... 040:     }          ... 042:     class Building 043:     {              ... 049:     } 050: } 051: 052: namespace BankSimulation 053: { 054:     using System.Collections; 055:     class Account 056:     { 057:         private ElevatorSimulation.Person accountholder;              ... 060:     } 061:     class Bank 062:     {              ... 070:         public void PrintBankDetails() 071:         {                  ... 075:             Console.WriteLine("Bank name: ..."); 080:         } 090:     } 091:     class Building 092:     {              ... 100:     } 101: } 102:  103: class MyGlobalNamespaceClass 104: {        ... 120: } 

Note: The line numbers are somewhat arbitrary (but always increasing) because of the code fragments that are not shown. This code does not compile.

There are a few important points to notice about the source code in Listing 15.2:

  • Compilation units can consist of using directives (see line 1), namespace definitions (lines 3 50 and 52 101) and class (or any other type) definitions (lines 103 120) written outside any explicitly specified namespaces. The using directives existing beside the namespaces must always precede the namespaces and the classes.

  • By placing a class in a namespace, we effectively change its name to a long name, which consists of the namespaces within which it resides separated by periods (.). For example, the class defined as Building in lines 42 49 is called ElevatorSimulation.Building, which distinguishes it from the BankSimulation.Building class defined in lines 91 100. This clearly demonstrates the namespace's ability to avoid name clashes.

  • Any using directive specified outside of any explicitly defined namespace (like using System; in line 1) covers all the classes written inside this compilation unit whether they belong to an explicitly defined namespace or not. So line 1 allows any of the methods inside any of the classes in the ElevatorSimulation and BankSimulation namespaces and inside the MyGlobalNamespaceClass to use, for example, the Console class as in line 75 without using its fully qualified name System.Console.

  • Any using directive specified inside an explicitly specified namespace only covers code that is written within the scope of this namespace. For example, only the classes defined within the BankSimulation namespace are affected by line 54 and can use the classname shortcuts from the System.Collections.

  • To reference a class in code that is written outside of the namespace where the referenced class is defined, you must either import the namespace name with the using directive into the namespace where the code you are writing resides, or you must write its fully qualified name, as in line 57 (ElevatorSimulation.Person refers to the Person class of the ElevatorSimulation namespace). To save you from writing ElevatorSimulation. in line 57, you could have inserted the following line after line 54:

     using ElevatorSimulation; 
  • If you want to reference a class from code written inside of the namespace where this class has been defined, you merely need to write the name of the class. This is demonstrated in line 10, which refers to the Person class defined in the same ElevatorSimulation namespace.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: