Information Technology Reference
In-Depth Information
Nesting Namespaces
A namespace can be a member of another namespace. The member is called a nested
namespace . Nesting namespaces allows you to create a conceptual hierarchy of types.
The nested namespace is a member of the enclosing namespace. However, the members
of the nested namespace are not members of the enclosing namespace. That is, members of
the enclosing namespace cannot automatically “see” the members of the nested namespace.
There are two ways of declaring a nested namespace:
￿
Textual nesting : You can create a nested namespace by placing its declaration inside the
declaration body of the enclosing namespace. This is illustrated on the left in Figure 10-9.
In this example, namespace OtherNS is nested in namespace MyNamespace .
￿
Separate declaration : You can also create a separate declaration for the nested
namespace, but you must use its fully qualified name in the declaration. This is illus-
trated on the right in Figure 10-9. Notice that in the declaration of nested namespace
OtherNS , the fully qualified name MyNamespace.OtherNS is used.
Figure 10-9. The two forms of declaring a nested namespace are equivalent.
Both forms of the nested namespace declarations shown in Figure 10-9 produce the
same assembly, as illustrated in Figure 10-10. The figure shows the two classes declared in file
SomeLib.cs , with their fully qualified names.
Figure 10-10. Nested namespace structure
Search WWH ::




Custom Search