Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Name Spaces

Posted by Ravi Varma Thumati on February 19, 2009

What is a namespace?

A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a developer does not need to worry about the naming conflicts of classes, functions, variables etc., inside a project.

   In Microsoft .Net, every program is created with a default namespace. This default namespace is called as global namespace. But the program itself can declare any number of namespaces, each of them with a unique name. The advantage is that every namespace can contain any number of classes, functions, variables and also namespaces etc., whose names are unique only inside the namespace. The members with the same name can be created in some other namespace without any compiler complaints from Microsoft .Net.

Namespaces are a way to define the classes and other types of information into one hierarchical structure. System is the basic namespace used by every .NET code. If we can explore the System namespace little bit, we can see it has lot of namespace uses the system namespace.

For example: System.Io, System.Net, System.Collections, System.Threading, etc.

A namespace can be created via the Namespace keyword. Here is an example to create “Books” namespace in VB.NET and C#.

VB.NET Code:

Namespace Books
            Class Authors
                        ‘Do something
           
End Class
End
Namespace

C# Code:

namespace Books
{
           
class Authors
            {
                       
//Do something
           
}
}

 

Creating and using Namespaces in VB.NET and C#

This is simple namespace example. We can also build hierarchy of namespace. Here is an example for this.

VB.NET Code:

Namespace Books
            
Namespace Inventory
            
Imports System
           
Class AddInventory
                       
Public Function MyMethod()
                                    Console.WriteLine(“Adding Inventory via MyMethod!”)
                       
End Function
            End Class
            End Namespace
End
Namespace

C# Code:

using System;
namespace Books
{
            
namespace Inventory
            {
                        class AddInventory
                        {
                                   
public void MyMethod()
                                    {
                                                Console.WriteLine(“Adding Inventory via MyMethod!”);
                                    }
                        }
            }
}

That’s all it takes to create namespace. Let’s look how we can use the namespaces in our code. I’m going to create a standalone program to use the namespaces.

VB.NET Code:

Imports System
Class HelloWorld
           
Public Sub Main()
                       
Dim AddInv As Inventory.AddInventory = New AddInventory
                        AddInv.MyMethod()
           
End Sub
End
Class

OR

Imports System.Inventory
Class HelloWorld
           
Public Sub Main()
                       
Dim AddInv As AddInventory = New AddInventory
                        AddInv.MyMethod()
           
End Sub
End
Class

 

C# Code:

using Books;
class HelloWorld
{
           
public static void Main()
            {
                        Inventory.AddInventory AddInv =
new AddInventory();
                        AddInv.MyMethod();
            }
}

OR

using Books.Inventory;
class HelloWorld
{
           
public static void Main()
            {
                        AddInventory AddInv =
new AddInventory();
                        AddInv.MyMethod();
            }
}

Note: When using Imports statement or Using statement we can use only the namespace names, we can’t use the class names. For example, the following statements are invalid.

Imports Books.Inventory.AddInventory

using Books.Inventory.AddInventory;

Final Notes

The namespaces are building blocks for the .NET way of software development. The namespaces wide opens the doors for 3’d party and project specific custom components and class libraries.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: