Namespaces
A namespace is a domain within which type names must be unique. Types are typically organized
into hierarchical namespaces—both to
avoid naming conflicts and to make type names easier to find. For example,
the RSA
type that handles public key
encryption is defined within the following namespace:
System.Security.Cryptography
A namespace forms an integral part of a type’s name. The following
code calls RSA
’s Create
method:
System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
Note
Namespaces are independent of assemblies, which are units of deployment such as an .exe or .dll.
Namespaces also have no impact on member accessibility: public
, internal
, private
, and so on.
The namepace
keyword defines a namespace for types within that block. For
example:
namespace Outer.Middle.Inner { class Class1 {} class Class2 {} }
The dots in the namespace indicate a hierarchy of nested namespaces. The code that follows is semantically identical to the preceding example.
namespace Outer { namespace Middle { namespace Inner { class Class1 {} class Class2 {} } } }
You can refer to a type with its fully qualified
name, which includes all namespaces from the outermost to the
innermost. For example, we could refer to Class1
in the preceding example as Outer.Middle.Inner.Class1
.
Types not defined in any namespace are said to reside in the
global namespace. The global namespace also includes
top-level namespaces, such as Outer
in
our example.
The using Directive
The using
directive ...
Get C# 4.0 Pocket Reference, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.