NS2
is nested within
NS1
. Thus, to refer to
ClassB
, you
must qualify it with both the
NS1
and
NS2
namespaces.
NS2
, by itself, is insufficient. As
shown, the namespace names are separated by a period. Therefore, to refer to
ClassB
within
Main( )
, you must use
NS1.NS2.ClassB
.
Namespaces can be nested by more than two levels. When this is the case, a member in
a nested namespace must be qualified with all of the enclosing namespace names.
You can specify a nested namespace using a single
namespace
statement by separating
each namespace with a period. For example,
namespace OuterNS {
namespace InnerNS {
// ...
}
}
can also be specified like this:
namespace OuterNS.InnerNS {
// ...
}
The Global Namespace
If you don’t declare a namespace for your program, then the default global namespace is
used. This is why you have not needed to use
namespace
for the programs in the preceding
chapters. Although the global namespace is convenient for the short, sample programs
found in this book, most real-world code will be contained within a declared namespace.
The main reason for encapsulating your code within a declared namespace is that it
prevents name conflicts. Namespaces are another tool that you have to help you organize
programs and make them viable in today’s complex, networked environment.
Using the :: Namespace Alias Qualifier
Although namespaces help prevent name conflicts, they do not completely eliminate them.
One way that a conflict can still occur is when the same name is declared within two
different namespaces, and you then try to bring both namespaces into view. For example,
assume that two different namespaces contain a class called
MyClass
. If you attempt to bring
these two namespaces into view via
using
statements,
MyClass
in the first namespace will
conflict with
MyClass
in the second namespace, causing an ambiguity error. In this situation,
you can use the
::
namespace alias qualifier
to explicitly specify which namespace is intended.
www.freepdf-books.com
448
P a r t I :
T h e C # L a n g u a g e
The :: operator has this general form.
namespace-alias
::
identifi er
Here,
namespace-alias
is the name of a namespace alias and
identifier
is the name of a member
of that namespace.
To understand why the namespace alias qualifier is needed, consider the following
program. It creates two namespaces,
Counter
and
AnotherCounter
, and both declare a class
called
CountDown
. Furthermore, both namespaces are brought into view by
using
statements.
Finally, in
Main( )
, an attempt is made to instantiate an object of type
CountDown
.
// Demonstrate why the :: qualifier is needed.
//
// This program will not compile.
using System;
// Use both the Counter and AnotherCounter namespace.
using Counter;
using AnotherCounter;
// Declare a namespace for counters.
namespace Counter {
// A simple countdown counter.
class CountDown {
int val;
public CountDown(int n) {
val = n;
}
// ...
}
}
// Declare another namespace for counters.
namespace AnotherCounter {
// Declare another class called CountDown, which
// is in the AnotherCounter namespace.
class CountDown {
int val;
public CountDown(int n) {
val = n;
}
// ...
}
}
class WhyAliasQualifier {
static void Main() {
int i;
www.freepdf-books.com
Do'stlaringiz bilan baham: |