C# | Namespaces

Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger .Net programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names) different from other sets of names. The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features. The members of a namespace can be namespaces, interfaces, structures, and delegates.

Defining a Namespace

To define a namespace in C#, we will use the namespace keyword followed by the name of the namespace and curly braces containing the body of the namespace as follows:

Syntax:



namespace name_of_namespace {

// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates

}

Example:

// defining the namespace name1
namespace name1 
{

    // C1 is the class in the namespace name1
    class C1
    {
         // class code
    }
}

Accessing the Members of Namespace

The members of a namespace are accessed by using dot(.) operator. A class in C# is fully known by its respective namespace.

Syntax:

[namespace_name].[member_name]

Note:

  • Two classes with the same name can be created inside 2 different namespaces in a single program.
  • Inside a namespace, no two classes can have the same name.
  • In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of namespaces
  
// namespace declaration
namespace first {
      
    // name_1 namespace members
    // i.e. class
    class Geeks_1 
    {
          
        // function of class Geeks_1
        public static void display()
        {
            // Here System is the namespace
            // under which Console class is defined
            // You can avoid writing System with 
            // the help of "using" keyword discussed
            // later in this article
            System.Console.WriteLine("Hello Geeks!");
  
        }
    }
      
      
    /* Removing comment will give the error
       because no two classes can have the 
       same name under a single namespace
      
    class Geeks_1
    {
          
    }       */
      
      
} // ending of first namespace
  
  
// Class declaration
class Geeks_2
{
      
    // Main Method
    public static void Main(String []args)
    {
          
        // calling the display method of 
        // class Geeks_1 by using two dot
        // operator as one is use to access 
        // the class of first namespace and 
        // another is use to access the 
        // static method of class Geeks_1.
        // Termed as fully qualified name 
        first.Geeks_1.display();        
          
    
}

chevron_right


Output:

Hello Geeks!

In the above example:

  • In System.Console.WriteLine()” “System” is a namespace in which we have a class named “Console” whose method is “WriteLine()“.
  • It is not necessary to keep each class in C# within Namespace but we do it to organize our code well.
  • Here “.” is the delimiter used to separate the class name from the namespace and function name from the classname.

The using keyword

It is not actually practical to call the function or class(or you can say members of a namespace) every time by using its fully qualified name. In the above example, System.Console.WriteLine(“Hello Geeks!”); and first.Geeks_1.display(); are the fully qualified name. So C# provides a keyword “using” which help the user to avoid writing fully qualified names again and again. The user just has to mention the namespace name at the starting of the program and then he can easily avoid the use of fully qualified names.

Syntax:

using [namespace_name][.][sub-namespace_name];

In the above syntax, dot(.) is used to include subnamespace names in the program.


Example:

// predefined namespace name
using System;

// user-defined namespace name
using name1

// namespace having subnamespace
using System.Collections.Generic;

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of using keyword
  
// predefined namespace
using System;
  
// user defined namespace
using first;
  
// namespace declaration
namespace first {
      
    // name_1 namespace members
    // i.e. class
    class Geeks_1 
    {
          
        // function of class Geeks_1
        public static void display()
        {
            // No need to write fully qualified name
            // as we have used "using System"
            Console.WriteLine("Hello Geeks!");
          
        }
    }
      
      
} // ending of first namespace
  
  
// Class declaration
class Geeks_2
{
      
    // Main Method
    public static void Main(String []args)
    {
          
        // calling the display method of 
        // class Geeks_1 by using only one 
        // dot operator as display is the 
        // static method of class Geeks_1
        Geeks_1.display();
                 
    
}

chevron_right


Output:

Hello Geeks!

Nested Namespaces

You can also define a namespace into another namespace which is termed as the nested namespace. To access the members of nested namespace user has to use the dot(.) operator.

For example, Generic is the nested namespace in the collections namespace as System.Collections.Generic

Syntax:

namespace name_of_namespace_1 
{
   
   // Member declarations & definitions
   namespace name_of_namespace_2 
   {

        // Member declarations & definitions
        .
        .

   }
}

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate use of 
// nested namespace
using System;
  
// You can also use 
// using Main_name.Nest_name;
// to avoid the use of fully 
// qualified name 
  
// main namespace
namespace Main_name
{            
      
    // nested namespace
    namespace Nest_name
    {
          
        // class within nested namespace      
        class Geeks_1
         {
               
              // Constructor of nested
              // namespace class Geeks_1
              public Geeks_1() { 
                    
                  Console.WriteLine("Nested Namespace Constructor");
                    
              }
        }   
    }                                                    
}
  
// Driver Class
class Driver 
{
      
    // Main Method
    public static void Main(string[] args)
    {
          
        // accessing the Nested Namespace by
        // using fully qualified name
        // "new" is used as Geeks_1() 
        // is the Constructor  
        new Main_name.Nest_name.Geeks_1();
          
    }    
}

chevron_right


Output:

Nested Namespace Constructor


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.