Namespaces
Namespaces are the
way that .NET avoids name clashes between classes. They are
designed to avoid the situation in which you define a class to
represent a customer, name your class Customer, and then someone else does the same thing
(a likely scenario - the proportion of businesses that have
customers seems to be quite high).
A namespace is no more than a grouping of data
types, but it has the effect that the names of all data types
within a namespace are automatically prefixed with the name of the
namespace. It is also possible to nest namespaces within each
other. For example, most of the general-purpose .NET base classes
are in a namespace called System. The
base class Array is in this namespace,
so its full name is System.Array.
.NET requires all types to be defined in a
namespace; for example, you could place your Customer class in a namespace called YourCompanyName. This class would have the full name
YourCompanyName.Customer.
|
|
Tip |
If a namespace is not explicitly supplied,
the type will be added to a nameless global namespace.
|
Microsoft recommends that for most purposes you
supply at least two nested namespace names: the first one refers to
the name of your company, and the second one refers to the name of
the technology or software package that the class is a member of,
such as YourCompanyName.SalesServices.Customer. This
protects, in most situations, the classes in your application from
possible name clashes with classes written by other
organizations.
Chapter 2, “C# Basics,” looks more
closely at namespaces.