Namespace And Nested Namespace In C#

In this blog, I will demonstrate how Namespaces are used in C#
 
Namespaces are used in C# programming in two ways,

Namespaces are used to use or include various classes of .Net Framework. A namespace can only be applied by using "using" keyword. We can include any number of Namespaces.
 
For Example, in order to use -
  1. Console.Write("hello Tutpoint");  
We have to first define namespace as
  1. using System;  
Here, 'System' is a namespace, 'Console' is the class of that namespace, and 'write' is the method of the console class. So, in order to use Write() method of Console class directly, we have defined the namespace. We have another way to do this as shown below,
  1. System.Console.Write("hello Tutpoint");  
While programming, we declare our own namespaces to control the scope of class and method names. Use the 'namespace' keyword to declare a namespace. As shown in the below example, we have declared a namespace named "Tutpoint":
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("hello");  
  10.         }  
  11.     }  
  12. }  
Nested Namespace is defined as namespace inside a namespace. Syntax for nested namespace is shown below:
 
Here,  'namespace_name2 ' is a namespace inside 'namespace_name1 ' namespace.
  1. namespace namespace_name1  
  2. {  
  3.     // code declarations  
  4.     namespace namespace_name2  
  5.     {  
  6.         // code declarations  
  7.     }  
  8. }  
Example of Nested namespace:
  1. using System;  
  2. using Tutpoint.InsideTutpoint;  
  3.   
  4. namespace Tutpoint  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             InternalProgram internalProgram = new InternalProgram();  
  11.             Console.WriteLine("hello");  
  12.             Program.num();  
  13.             internalProgram.num();  
  14.             Console.ReadLine();  
  15.         }  
  16.   
  17.         public static void num()  
  18.         {  
  19.             Console.WriteLine("Inside Main Namespace");  
  20.         }  
  21.     }  
  22.   
  23.     namespace InsideTutpoint  
  24.     {  
  25.         public class InternalProgram  
  26.         {  
  27.             public void num()  
  28.             {  
  29.                 Console.WriteLine("Inside Second Namespace");  
  30.             }  
  31.         }  
  32.   
  33.     }  
  34.   
  35. }  
Here, namespace "InsideTutpoint" is defined inside namespace "Tutpoint". We can access members of the nested namespace by using the dot (.) operator.
 
Output of the program is,
  • hello
  • Inside Main Namespace 
  • Inside Second Namespace  
In the example shown above, we have declared a namespace as "using Tutpoint.InsideTutpoint;". If we do not declare this namespace and in order to access the class of nested namespace, we have to use dot(.) operator as namespace_name.ClassName as shown below:
  1. InsideTutpoint.InternalProgram internalProgram = new InsideTutpoint.InternalProgram();  
Next Recommended Reading Nested Namespaces in C#