Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Namespaces in ASP.NET

Author: Richard Wand

This article will give you a good understanding of Namespaces and their usage in ASP.NET.

 

 
Namespace is an important and excellent feature available in ASP.NET. Like all object oriented programming languages, classes and objects are used in ASP.NET languages to organize programs and applications. Namespace is like a container of classes, objects, structures, enumerators, interfaces and delegates.  Namespaces allow us to organize classes so that they can be easily accessed in other applications. It is used to group related classes in hierarchical structure. Namespaces allow you to create a system to organize your code in a hierarchy. It is only a way of grouping related classes so that they can be distinguished from other classes.
 
Namespaces are the collection of classes and objects. Classes and objects are grouped according to their functionality in a namespace and different functionality classes can be included in different namespaces. For example in .NET, classes to work with files are included in System.IO namespace and similarly classes to work with data are included in System.Data namespace. “System” namespace is the basic namespace used in all .NET applications. All other namespaces exist in System namespace. There are so many classes in .NET framework that there should be a way to organize them. Namespace is the way to organize these classes for easy access. This namespace functionality is also provided for developers so that they can create their own namespaces and organize their classes within these namespaces. Developers use namespaces when they want to place their classes within specific scope.
 
The keyword “namespace” is used to create a namespace and it will declare a scope for that namespace. This namespace gives you a scope to create classes and organize your code within classes and function. A namespace can contain any number of classes, functions and even namespaces. You can define a namespace within a namespace. So namespaces can be created in a nested way. Most general namespace can be created at the top level of the hierarchy and most specific at the bottom of the hierarchy. Names of classes and function are unique inside a namespace but you can create classes and functions in other namespace with same name. Naming for the namespace is not important; you can write it according to your desire.
 
All code is created inside a namespace. Even if you don’t explicitly create a namespace, .NET framework automatically create a default namespace which is used for any code written outside explicitly declared namespace. This default namespace is called global namespace.
 
 
Benefits of namespaces

  • It helps to manage your code in classes.
  • Developer does not need to worry for naming conflicts classes, functions and variables in a project.
  • Namespaces helps to reuse your code and save you from problems.
  • We can have nested namespaces, namespaces within namespaces.
  • Any number of classes, functions and namespaces can exists inside a namespace.

 

Difference between Namespace and Assembly

A namespace is the logical grouping of classes and the physical grouping of these classes is called assembly. Assemblies are often called class libraries because they contain classes that you can use in your program. Assemblies have .dll file extension. Namespace is a collection of name where each name is unique and assembly is a collection of functionality that is built as a single implementation unit.  A namespace is a logical design time naming convention while an assembly establishes the name scope at run time. A namespace can be contained by one or more assemblies and one assembly can contain one or more namespaces.
 
Namespaces Examples

You can create namespaces using “namespace” keyword and you can create one or more that one class inside namespaces. 

 

C#

namespace Namespace1
{
    public class class1
    {
        public void Func1()
        {
            //do something here
        }
    }
}
 
namespace Namespace2
{
    public class class2
    {
        public void Func2()
        {
            //do something here
        }
    }
    public class class3
    {
        public void Func3()
        {
            //do something here
        }
    }
}
 
VB.NET

Namespace Namespace1
    Public Class class1
        Public Sub Func1()
            'do something here
        End Sub
    End Class
End Namespace
 
Namespace Namespace2
    Public Class class2
        Public Sub Func2()
            'do something here
        End Sub
    End Class
    Public Class class3
        Public Sub Func3()
            'do something here
        End Sub
    End Class
End Namespace
 
To call Functions defined inside classes, we need to create object of the class using the namespace of that class.

C#

Namespace1.class1 obj1 = new Namespace1.class1();
obj1.Func1();
 
Namespace2.class2 obj2 = new Namespace2.class2();
obj2.Func2();
 
Namespace2.class3 obj3 = new Namespace2.class3();
obj3.Func3();
 
VB.NET

Dim obj1 As New Namespace1.class1()
obj1.Func1()
 
Dim obj2 As New Namespace2.class2()
obj2.Func2()
 
Dim obj3 As New Namespace2.class3()
obj3.Func3()
 
You can create nested namespaces as well

C#

namespace Namespace1
{
    public class class1
    {
        public void Func1()
        {
            //do something here
        }
    }
 
    namespace Namespace2
    {
        public class class2
        {
            public void Func2()
            {
                //do something here
            }
        }
    }
}
 
VB.NET
 
Namespace Namespace1
    Public Class class1
        Public Sub Func1()
            'do something here
        End Sub
    End Class
 
    Namespace Namespace2
        Public Class class2
            Public Sub Func2()
                'do something here
            End Sub
        End Class
    End Namespace
End Namespace
 
You can call Functions defined in the nested namespace class by referencing namespaces like below.

C#

Namespace1.class1 obj1 = new Namespace1.class1();
obj1.Func1();
 
Namespace1.Namespace2.class2 obj2 = new Namespace1.Namespace2.class2();
obj2.Func2();
  
VB.NET

Dim obj1 As New Namespace1.class1()
obj1.Func1()
 
Dim obj2 As New Namespace1.Namespace2.class2()
obj2.Func2()
 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use jQuery Lightbox in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net