C# Namespaces


  • In C# Namespaces are "named group of classes".
  • A Namespace defines a declarative region that provides a way to separate the one set of names to another set of names.
  • Names declared in one Namespace will not conflict with the same name declared in the other Namespace.
Declaration of Namespace
  • A Namespace is declared with the keyword "namespace".
  • A namespace declaration defines a scope. Anything declared immediately inside the namespace is in scope throughout the namespace.
  • Inside a namespace, you can declare classes, structures, delegates, enumerations, interfaces, or another namespace.
  • The basic structure to declare a Namespace is given below:
namespace name
{
Members//
}

Here name is the name given to the namespace and member can be anything as class, structures, delegates, enumeration, interfaces or another namespaces. Here is a simple example of declaration of Namespace:


namespace HellowordApplication
{
……………..
}

You can call the item present in namespace as:


namespace_name.itemname

To understand the use of Namespace please go through the following example:


Example

using System;
namespace additionoperation//namespace declaration
{
class adding
{
public void add(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("\nInside namespace additionoperation");
Console.WriteLine("\nResult of addition:{0}",c);
}
}
}
namespace addoperation//namespace declaration
{
class adding
{
public void add(int a,int b)
{
int c;
c=a+b;
Console.WriteLine("\nInside namespace addoperation");
Console.WriteLine("\nResult of addition:{0}",c);
}
}
}
namespace operation
{
class op
{
static void Main()
{
additionoperation.adding a1 = new additionoperation.adding();//calling of namespace member
addoperation.adding a2 = new addoperation.adding();//calling of namespace member
a1.add(5, 6);
a2.add(6,7);
Console.ReadLine();
}
}
}

The ouput of the above program would be as follows:


Output

Inside namespace additionoperation
Result of addition:11
Inside namespace addoperation
Result of addition:13

using keyword


  • 'using' keyword enable the user to use the member present inside a namespace.
  • The namespace used by the .NET Framework C# library is System. All the member that are present in System namespace are used by the using keyword as given below:
  • using System;

The following program reworks the example from the previous section to show how you can employ using to bring a namespace that you create, just change the class name adding present in addoperation to adding1 to avoid conflict:


Example

using System;
using additionoperation;
using addoperation;
namespace additionoperation//namespace declaration
{
class adding
{
public void add(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("\nInside namespace additionoperation");
Console.WriteLine("\nResult of addition:{0}",c);
}
}
}
namespace addoperation//namespace declaration
{
class adding1
{
public void add(int a,int b)
{
int c;
c=a+b;
Console.WriteLine("\nInside namespace addoperation");
Console.WriteLine("\nResult of addition:{0}",c);
}
}
}
namespace operation
{
class op
{
static void Main()
{
adding a1 = new adding();
adding1 a2 = new adding1();
a1.add(5, 6);
a2.add(6,7);
Console.ReadLine();
}
}
}

The output of the above program will be as follows:


Output

Inside namespace additionoperation
Result of addition:11
Inside namespace addoperation
Result of addition:13

Nested Namespaces


  • One namespace can be nested within another.
  • When referring to a nested namespace from code outside the nested namespaces, both the outer namespace and the inner namespace must be specified, with the two separated by a period.
  • Here is a basic structure of nested namespace:
  • namespace outernamspace_name
    {
    //members
    namespace innernamespace_name
    {
    //members
    }
    }

  • Here is an example of Nested namespace as follows:
  • Example

    using System;
    namespace Outer
    {
    class classA
    {
    public classA()
    {
    Console.WriteLine("Constructing ClassA");
    }
    }
    namespace Inner//Nested Namespace
    {
    class classB
    {
    public classB()
    {
    Console.WriteLine("Constructing ClassB");
    }
    }
    }
    }
    class Nestednamespace
    {
    static void Main()
    {
    Outer.classA a = new Outer.classA();
    //Inner.classB b=new Inner.classb();//It would be an error
    Outer.Inner.classB b = new Outer.Inner.classB();
    Console.ReadLine();
    }
    }

  • The output of the above program will be as follows:
  • Output

    Constructing ClassA
    Constructing ClassB

Global Namespace


  • When you don't declare a namespace for your program, the default Global namespace is used in that case.
  • It means it is not necessary to declare namespace for the program the compiler will consider the default global namespace itself.
  • The concept of global namespace is a good idea for the short and simple program but for the large and complex program you have to declare the namespace to avoid conflicts.