Namespaces in C#

The namespace is collection of same set of related objects or same set of types. If you forget to declare namespace explicitly, then C# compiler adds a default namespace. This default namespace is called as global namespace. All namespaces are implicitly public and you cannot change this behavior. A namespace declaration does not support modifiers or attributes.

A namespace begins with the keyword namespace and followed by the namespace name as follows:

namespace namespace_name
{
   // Your code here
}


Within a namespace, you can declare one or more of the following types:

  • another namespace
  • class
  • interface
  • struct
  • enum
  • delegate

namespace MyNamespace
{
    class MyClass { }
    interface MyInterface { }
    struct MyStruct { }
    enum Days { a, b }
    delegate void MyDelegate();
    namespace MyNamespace.NestedNamespace
    {
        class Employee { }
    }
}


Namespaces Prevent Name Conflicts

A namespace defines a scope or boundary. The whole thing that is declared inside the namespace is in scope throughout the namespace.

Example

using System;
namespace ConsoleApplication1
{
    namespace firstNamespace
    {
        class TutorialRide
        {
            int count = 1;
          public TutorialRide()
            {
                count++;
            }
            public void show()
            {
                Console.WriteLine("I am in First Namespace and count = " + count);
            }
        }
    }
    namespace secondNamespace
    {
        class TutorialRide
        {
            int count = 2;
           public TutorialRide()
            {
                count++;
            }
            public void show()
            {
                Console.WriteLine("I am in Second Namespace and count = " + count);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            firstNamespace.TutorialRide obj1 = new firstNamespace.TutorialRide();
            secondNamespace.TutorialRide obj2 = new secondNamespace.TutorialRide();
            obj1.show();
            obj2.show();
            Console.ReadLine();
        }
    }
}


The above example is quite simple but it shows how namespace helps to prevent name conflicts. There are two different namespaces, firstNamespace and secondNamespace. In both the namespaces we have created class with same name as TutorialRide. Because both classes are in separate namespaces, you will not get any error of name conflicting. In the above code only class name is not same but the method name is also same.  There is method with same name as show() in both the classes. For accessing these methods you have to use fully qualified name as given below:

firstNamespace.TutorialRide obj1 = new firstNamespace.TutorialRide();
secondNamespace.TutorialRide obj2 = new secondNamespace.TutorialRide();

“using” directive

Already created namespaces you used by “using”  directive. As example when you declare as “using System;” at starting of your application, then all the accessible member of system namespace you can access. For example Console class is available in System namespace. If you added System namespace, then Console class will be available in that .cs file to use. It is not compulsory to write “using System;” directive at the top of program in C#. But in this case, whenever you want to write Console class, you have to write fully qualified name as:

System.Console.WriteLine();
System.Console.ReadLine();
System.IO.FileStream fs;
   
                        
The above code is cumbersome. Therefore it is better to use namespace at the beginning of program with the help of “using” directive.

Another use of “using” directive is that it creates another name, called as alias, for a type or a namespace as example.

using MyNamespace = ConsoleApplication1.firstNamespace;

Now you can use all classes that are available in firstNamespace using MyNamespace.

Namespaces can be nested

You can write namespace within another namespace.

Syntax

namespace firstNamespace
{
   // Your code declarations.
   namespace secondNamespace
   {
      // Your code declarations.
   }
}


Members of nested namespace can be access by using the dot (.) operator.

Example

using System;
    namespace firstNamespace
    {
        class A
        {
            public void show()
            {
                Console.WriteLine("Inside first namespace");
            }
        }
        namespace secondNamespace
        {
            class B
            {
                public void show()
                {
                    Console.WriteLine("Inside second namespace");
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            firstNamespace.A objA = new firstNamespace.A();
            firstNamespace.secondNamespace.B objB = new firstNamespace.secondNamespace.B();
            objA.show();
            objB.show();
            Console.ReadLine();
        }
    }


In the above program, the namespace secondNamespace is nested within firstNamespace. Therefore, to access class B, you must qualify it with both the firstNamespace and secondNamespace namespaces. In the above code, the namespace names are separated by a period. Therefore, to refer to ClassB within  Main( ), you must use firstNamespace.secondNamespace.B. You can access the class A member by creating the object as follows:

firstNamespace.A objA = new firstNamespace.A();

Let us understand one more example, related to nesting of namespace.

Example

using System;

using A.B.C;
namespace A
{
    namespace B
    {
        namespace C
        {
            public class Demo1
            {
                public void function1()
                {
                    Console.WriteLine("Function1 in namespace A.B.C ");
                }
            }
        }
    }
}
namespace D
{
    public class Demo2
    {
        public void function2()
        {
            Console.WriteLine("Function2 in namespace D ");
        }
    }
}
namespace E
{
    public class Demo3
    {
        public void function3()
        {
            Console.WriteLine("Function3 in namespace E ");
        }
    }
}
namespace F
{
    using D;
    class Program
    {
        static void Main(string[] args)
        {
            // Can access CClass type directly from A.B.C.
            Demo1 objDemo1 = new Demo1();
            Demo2 objDemo2 = new Demo2();
            E.Demo3 objDemo3 = new E.Demo3();
            objDemo1.function1();
            objDemo2.function2();
            objDemo3.function3();    
            Console.ReadLine();
        }
    }
}


Run the above application you will get the following output.

Function1 in namespace A.B.C
Function2 in namespace D
Function3 in namespace E

Demo1 class is available in namespace C, namespace C is available in B and B namespace is nested within A. Therefore accessing the Demo1 class, you can use fully qualified name as A.B.C.Demo1. Second option is we can use a single line code as used in our code.

“using A.B.C;”. In this case there is no need to write fully qualified name.

Demo2 class can also be accessed directly because we have used “using D;” in namespace F.

In third case, for accessing Demo3 class, you have to write fully qualified name as

E.Demo3 objDemo3 = new E.Demo3();

Because we have not used statement “using E” in namespace F.