Section 2.3. Namespaces


2.3. Namespaces

The notion of a namespace plays a fundamental role in the .NET Framework. In general, a namespace is a logical grouping of types (classes and similar constructs) for the purposes of identification and navigation. There are so many classes and features in .NET that there are bound to be name conflicts. And since third-party libraries can be integrated into the class space just like the Microsoft-supplied libraries, namespaces keep everything neat and orderly.

Imagine that, in a certain business, there is an executive named John Smith, a secretary named John Smith, and a custodian named John Smith. In this case, the name John Smith is ambiguous. When the paymaster stands on a table and calls out the names of people to receive their paychecks, the executive John Smith won't be happy if he rushes to the table when custodian John Smith's paycheck is in the paymaster's hand.

To resolve the naming ambiguity, the business can define three namespaces: Executive, Secretarial, and Custodial. Now the three individuals can be unambiguously referred to by their fully qualified names:

  • Executive.John Smith

  • Secretarial.John Smith

  • Custodial.John Smith

Namespaces in .NET look a lot like these references to John Smith. They are simply names used to group and organize all of the .NET classes into a hierarchy. Namespaces can be nested. Consider the following three possible namespaces.

  • America Namespace

  • America.Washington Namespace

  • America.Washington.Seattle Namespace

Each of these namespaces can include classes (and other types) and additional namespaces. And the same class name can appear in multiple namespaces, even in nested namespaces.

  • America.Demographics Class

  • America.Washington.Demographics Class

  • America.Washington.Seattle.Demographics Class

  • America.Montana.Demographics Class

The .NET Framework Class Library (FCL) consists of several thousand classes and other types (such as interfaces, structures, and enumerations) that are divided into about 200 namespaces. All classes considered to be the "core" language-neutral classes of .NET appear in the System namespace, or in one of the nested namespaces within System. The namespaces supplied with .NET provide basic system services, such as:

  • Basic and advanced data types and exception handling (the System namespace)

  • Data access (the System.Data namespace)

  • User-interface elements for standard Windows applications (the System.Windows.Forms namespace)

  • User-interface elements for web applications (the System.Web.UI namespace)

Many Visual Basic language features are implemented within the classes of the Microsoft.VisualBasic namespace. (The C# and J# languages have corresponding namespaces.)

All classes (and other types) exist in a namespace, even the classes of your application. By default, your project's namespace is at the top of the hierarchy (next to System) and is named after your project's name. You can alter this by using the Namespace statement at the beginning of a code file, or by defining a different project namespace through the Project Properties.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net