C# Namespaces

Namespace can be define as a Collection of classes and Objects which is used to keep separate one set of names from another.The class or Method define in one Namespace will not conflict with class and methods define in another namespace.

System also use a set of Namespace. Like:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.IO;  
we can access all the classes and Method of a Namespace through “Using” Keyword.

The Using Keyword

The using keyword states that the program is using the names in the given namespace. For example, we are using the System namespace in our programs. The class Console is defined there. We just write:
  1. Console.WriteLine ("Hello World");  
We could have written the fully qualified name as if we are not using a Namespace :
  1. System.Console.WriteLine("Hello World!");  
Now we Understand Some user Define Namespaces. How can we create userdefine Namespaces:

Defining a Namespace

A namespace definition begins with the keyword namespace followed by the namespace name as follows:
  1. namespace Namespace_Name  
  2. {  
  3.    // code declarations  
  4. }  
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows:
  1. namespace_name.item_name;  
The following program demonstrates use of namespaces:

Method1 
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Collections;    
  5. using System.Text;    
  6. using System.Threading.Tasks;    
  7. //NameSpace Demo Start Here    
  8. namespace Demo1    
  9. {    
  10.    class Class_Demo1     
  11.    {    
  12.       public void func1()    
  13.       {    
  14.          Console.WriteLine("You are in Demo1 Namespace");    
  15.       }    
  16.    }    
  17. }    
  18.     
  19. // NameSpace Demo2 Start Here    
  20. namespace Demo2    
  21. {        
  22.    class Class_Demo2    
  23.    {    
  24.       public void func2()    
  25.       {    
  26.          Console.WriteLine("You are in Demo2 Namespace");    
  27.       }    
  28.    }      
  29. }    
  30.   
  31. namespace ConsoleApplication2    
  32. {    
  33.    class Program    
  34.    {    
  35.       public static void Main(string[] args)    
  36.       {    
  37.          Demo1.Class_Demo1 Obj1 = new Demo1.Class_Demo1();    
  38.          Demo2.Class_Demo2 Obj2 = new Demo2.Class_Demo2();    
  39.          Obj1.func1();    
  40.          Obj2.func2();    
  41.          Console.ReadLine();    
  42.       }    
  43.    }    
  44. }    
The Output will be:

You are in Demo1 Namespace

You are in Demo2 Namespace

Method2

We can also avoid prepending of namespaces with the using namespace directive. If we Include the Namespace in program then we can access classes of a Namespace Directly.
  1. using System;     
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Collections;    
  5. using System.Text;     
  6. using System.Threading.Tasks;      
  7. using Demo1;     
  8. using Demo2;    
  9. //NameSpace Demo Start Here    
  10. namespace Demo1    
  11. {    
  12.    class Class_Demo1    
  13.    {    
  14.       public void func1()    
  15.       {    
  16.          Console.WriteLine("You are in Demo1 Namespace");    
  17.       }    
  18.    }     
  19. }    
  20.     
  21. // NameSpace Demo2 Start Here    
  22.     
  23. namespace Demo2      
  24. {    
  25.    class Class_Demo2    
  26.    {    
  27.       public void func2()    
  28.       {    
  29.          Console.WriteLine("You are in Demo2 Namespace");    
  30.       }    
  31.    }     
  32. }    
  33. namespace ConsoleApplication2    
  34. {    
  35.    class Program    
  36.    {     
  37.       public static void Main(string[] args)    
  38.       {    
  39.          Class_Demo1 Obj1 = new Class_Demo1();    
  40.          Class_Demo2 Obj2 = new Class_Demo2();    
  41.          Obj1.func1();    
  42.          Obj2.func2();    
  43.          Console.ReadLine();    
  44.       }    
  45.    }    
  46. }  
Nested Namespaces

Namespaces can be nested where you can define one namespace inside another namespace as follows:
  1. namespace namespace_name1  
  2. {  
  3.    // code declarations  
  4.    namespace namespace_name2  
  5.    {  
  6.       // code declarations  
  7.    }  
  8. }  
The following program demonstrates this Concept:
  1. using System;      
  2. using System.Collections.Generic;      
  3. using System.Linq;      
  4. using System.Collections;      
  5. using System.Text;      
  6. using System.Threading.Tasks;  
  7. using Demo1;   
  8. using Demo1.Demo2;    
  9. //NameSpace Demo Start Here    
  10. namespace Demo1    
  11. {    
  12.    namespace Demo2    
  13.    {    
  14.       class Class_Demo2    
  15.       {    
  16.          public void func2()    
  17.          {    
  18.             Console.WriteLine("You are in Demo2 Namespace");    
  19.          }    
  20.       }    
  21.    }    
  22.    class Class_Demo1    
  23.    {    
  24.       public void func1()    
  25.       {    
  26.          Console.WriteLine("You are in Demo1 Namespace");    
  27.       }    
  28.    }    
  29. }    
  30. // NameSpace Demo2 Start Here    
  31. namespace ConsoleApplication2    
  32. {    
  33.    class Program    
  34.    {    
  35.       public static void Main(string[] args)    
  36.       {    
  37.          Class_Demo1 Obj1 = new Class_Demo1();    
  38.          Class_Demo2 Obj2 = new Class_Demo2();    
  39.          Obj1.func1();    
  40.          Obj2.func2();    
  41.          Console.ReadLine();    
  42.       }    
  43.    }    
  44. }    

Ebook Download
View all
Learn
View all