Different Ways To Access Namespace In C#

Class organization is facilitated through the utilization of namespaces. Within larger classes, namespaces play a crucial role in managing the accessibility of methods in the realm of internet programming. Put another way, namespaces serve as a tool for delineating between different groups of names, such as class names, ensuring clarity and organization. The primary advantage of namespaces lies in their ability to prevent clashes between class names that exist in separate namespaces. Another term used to describe this concept is a group of classes that share common attributes. Within a namespace, one can find a variety of members including namespaces themselves, interfaces, structures, and delegates.

Syntax:

It has the following syntax:

Example

namespace name_of_namespace {
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates

}

Example:

Let's consider a scenario to demonstrate how to retrieve the namespace in C#.

Example

// Redefined namespace MyNamespace
namespace MyNamespace 
{

    // MyClass is a class within the MyNamespace namespace
    class MyClass
    {
         // Modified class 
         private int myNumber;
         private string myText;

         // Constructor 
         public MyClass(int number, string text)
         {
             this.myNumber = number;
             this.myText = text;
         }

         // New method
         public void DisplayInfo()
         {
             Console.WriteLine($"Number: {myNumber}, Text: {myText}");
         }
    }
}

Accessing the Members of Namespace

The dot(.) operator is employed to retrieve members within a namespace. In C#, a class is uniquely identified by its namespace.

Syntax:

Example

It has the following syntax:</p>
<p>[namespace_name].[member_name]

Note:

  • Within a single program, two classes with the same name can be generated in two separate namespaces.
  • There cannot be a name conflict between two classes within a namespace.
  • Class names in C# are fully qualified names; they begin with the namespace name and end with the class name, separated by the dot(.) operator.
  • Using Keyword:

Employing the complete name of a function or class whenever we need to call it, or instead, elements of a namespace, is not particularly convenient. In the instances mentioned earlier, the complete names are first.Javhello world_1.display; and System.Console.WriteLine("Hello hello world");. The keyword "using" in C# is designed to help users avoid the repetitive use of fully qualified names. Users can easily circumvent the need for fully qualified names by simply specifying the namespace name at the beginning of the program.

Syntax:

It has the following syntax:

Example

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

Example:

Let's consider a scenario to demonstrate the utilization of the namespace with the "Using" keyword in C#.

Example

// predefined namespace name
using System;

// user-defined namespace name
using name1

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

Example Code:

Example

// C# program showcasing the use of the using keyword 

//Predefined namespace 
using System; 
// User-defined namespace 
using myNamespace; 

// Namespace declaration 
namespace myNamespace { 
	
	// Custom namespace members (class)
	class CustomClass 
	{ 
		
		// Function of the CustomClass 
		public static void PrintMessage() 
		{ 
			// No need to write fully qualified name 
			// as we have used "using System" 
			Console.WriteLine("Greetings from CustomClass!"); 
		} 
	} 
	
	
} // End of myNamespace namespace 


// Class declaration 
class AnotherClass 
{ 
	
	// Main Method 
	public static void Main(String []args) 
	{ 
		// Calling the PrintMessage method of 
		// CustomClass using only one dot operator 
		// since PrintMessage is a static method of CustomClass 
		CustomClass.PrintMessage(); 
	} 
}

Output:

Output

Greetings from CustomClass!

Nested Namespaces:

This concept is referred to as a nested namespace, which involves defining a namespace within another namespace. Accessing members of the nested namespace necessitates the use of the dot (.) operator. For instance, within the System namespace, Generic is an example of a nested namespace.

Example

System.Collections.Generic
namespace name_of_namespace_1 
{
   
   // Member declarations & definitions
   namespace name_of_namespace_2 
   {

        // Member declarations & definitions
        .
        .

   }
}

EXAMPLE CODE:

Let's consider a scenario to demonstrate the process of accessing the namespace through nested namespace function in C#.

Example

using System;

// Outer namespace
namespace OuterNamespace
{
    // Outer namespace class
    class OuterClass
    {
        public static void OuterMethod()
        {
            Console.WriteLine("OuterClass method in OuterNamespace");
        }
    }

    // Nested namespace
    namespace InnerNamespace
    {
        // Inner namespace class
        class InnerClass
        {
            public static void InnerMethod()
            {
                Console.WriteLine("InnerClass method in InnerNamespace");
            }
        }
    }
}

// Main class
class Program
{
    static void Main()
    {
        // Accessing members from the outer namespace
        OuterNamespace.OuterClass.OuterMethod();

        // Accessing members from the nested namespace
        OuterNamespace.InnerNamespace.InnerClass.InnerMethod();
    }
}

Output:

Output

OuterClass method in OuterNamespace
InnerClass method in InnerNamespace

Conclusion:

In summary, C# offers a range of methods to manage and customize namespace access, enhancing flexibility and control over naming conventions and code structure. The primary approach, the 'using' directive, simplifies the process of importing namespaces and utilizing their types. On the other hand, fully qualified names offer a direct way to access types without ambiguity.

To address lengthy or frequently used namespaces, alias directives enable the creation of custom shortcuts. In cases of naming conflicts, the 'global' modifier ensures unambiguous references. Nested namespaces establish a structured hierarchy for organizing related functionalities within broader namespaces. These practices underscore the importance of coherent and sustainable coding techniques in C#, guiding developers to select the most suitable approach based on the specific needs of their codebase.

Input Required

This code uses input(). Please provide values below: