What You Need to Know about Namespaces

Namespaces let you reuse class names without creating conflict.
Namespaces let you reuse class names without creating conflict. | Source

What Are Namespaces?

Namespaces are used to organize class libraries in several different programming languages.

Names declared in one namespace will not conflict with variable names declared in a separate namespace. Place classes in namespaces to prevent conflict with names in your code with the code written by others. Create namespace aliases to reduce the length of the namespace reference as it is entered and re-entered in the code.

The methods of declaring namespaces, creating namespace aliases and using sub-namespaces depend on the software language being used.

ASP.NET Namesspaces

The ASP.NET platform uses namespaces to organize base class libraries. Each namespace contains semantically related types within an assembly. For example, the system.IO namespace holds file I/O types such as those used for port manipulation or data compression.

The dot-net runtime uses the same namespaces and same types to run all programs, whereas language specific libraries like MFC use different namespaces and types.
The system.data namespace defines different database types. A single assembly like a dll file may contain several namespaces, while each namespace contains several types.

Nested namespaces fall under a rooted namespace. The system namespace, a root namespace, contains the core types used to build a software application. Nested namespaces under the root namespace have a description of the subcategory listed after a period.
The system.collections namespace defines stock containers and base types. Use the system.data namespace to interact with relational databases.

System.Web is used to build ASP.NET web apps. System.Security namespace holds types related to encryption and access controls. System.XML contains types define how XML data is handled. .NET runtime engines access namespaces.

Programmers explicitly list the namespaces used by files when creating a class, such as a bitmap class defined by the system.drawing namespace. Errors arise when a system.drawing namespace isn't specified but has been called.


Most .NET namespaces reside in the mscorlib.dll file. Many .NET assemblies reside in the global assembly cache directory, also called the GAC. Microsoft Windows' default location for this is the C:\Windows\Assembly folder.

C Namespaces

The C programming language uses namespaces to define declarative regions. Several versions of C use the dot net framework class library.


C allows programmers to use abbreviated namespaces. C programs recognize console.writeline as a reference to the system.console.writeline namespace. Programmers avoid the need to spell out the System namespace at the start of each namespace callout by including the phrase "using System" at the top of the program.


Declare a C namespace with the command "namespace name { //members }". The members can be classes, structures, interfaces and other namespaces. Qualify secondary namespaces with the name of the primary namespace, or the namespace will not be found by the code compiler. You can reference multiple nested namespaces by separating each namespace with a period. C programs without a namespace declared default to a global namespace.


Reference namespaces by aliases with the using directive to avoid spelling out the full namespace designation with the "using alias = name:" command.

C language extern methods handle unmanaged code and methods supplied by external code such as data supplied by a website. Declare extern aliases to create aliases for external assemblies so they do not conflict with your code's namespaces.

Namespaces are used to create Windows Forms.
Namespaces are used to create Windows Forms. | Source

Visual Basic and Visual Studio Namespaces

Visual Studio uses root namespaces called Microsoft and System. The Microsoft namespace is broken down into different programming languages like Visual Basic and C#. Visual Basic or VB programs gain access to namespaces after programmers add a reference to the correct dll library reference.

Create new namespaces with a Namespace statement. Define the classes within the new namespace before ending the statement. Visual Basic assumes qualified namespaces are fully qualified. Declarations fail if a namespace class is missing.

Visual Basic treats unresolved references as partially qualified and searches the current namespace for the variable. Visual Basic searches the next higher level on the namespace hierarchy for the class variable.


Use the Imports statement at the top of a Visual Basic program to avoid mentioning the namespace through the rest of the code.

Windows Forms automatically imports the System root, System.Drawing, System.Data, System.Window.Forms and Microsoft.Visual Basic namespaces. After importing a namespace, create an alias with a Dim command. Create an employee alias with the "Dim Employee As Jobnamespace.Jobclasses.Employee". Refer to the employee alias throughout the rest of the code instead of repeatedly spelling out the full namespace again.


Modify Visual Basic root namespaces through the Solution Explorer's Application tab.
Microsoft Windows Forms use controls defined by the System.Windows.Forms namespace. Both Windows Forms and WPF applications use namespaces to define buttons, data labels and check boxes.

References

  1. "Visual C# 2010 Recipes: A Problem-Solution Approach" by Allen Jones, Adam Freeman, Matthew MacDonald and Rakesh Rajan
  2. "Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition" by Andrew Troelsen
  3. "Pro VB 2010 and the .NET 4.0 Platform" by Andrew Troelsen and Vidya Vrat Agarwal
  4. "C# 4.0: The Complete Reference" by Herbert Schildt
  5. "Visual Basic 2010 Programmer's Reference" by Rod Stephens

More by this Author


Comments

No comments yet.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working