Class organisation is done by namespaces. In bigger classes, it helps in controlling the reach of methods.initiatives involving net programming. Stated differently, it offers a mechanism to maintain distinctions between one collection of names (such as class names) and other sets of names. Namespaces provide the main benefit of preventing class names from conflicting with one another when they are specified in different namespaces. An alternative name for it is titled set of classes with shared characteristics. Namespaces, interfaces, structures, and delegates can all be members of the same namespace.
Syntax:
It has the following syntax:
namespace name_of_namespace {
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}
Example:
Let us take an example to illustrate how to access the namespace in C#.
// 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 used to access members of a namespace. In C#, a class is fully identified by its namespace.
Syntax:
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:
Using the function or class's fully qualified name each time we want to invoke it or, alternatively, members of a namespace is not really practical. The fully qualified names in the example above are first.Javhello world_1.display; and System.Console.WriteLine("Hello hello world") ;. The term "using" is provided by C# to assist the user in avoiding repeatedly writing fully qualified names. Utilizing fully qualified names can be effortlessly avoided by the user by just mentioning the namespace name at programme startup.
Syntax:
It has the following syntax:
using [namespace_name][.][sub-namespace_name];
Example:
Let us take an example to illustrate how to access the namespace using (Using) Keyword in C#.
// predefined namespace name
using System;
// user-defined namespace name
using name1
// namespace having subnamespace
using System.Collections.Generic;
Example Code:
// 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:
Greetings from CustomClass!
Nested Namespaces:
This is known as a nested namespace, where you can define a namespace inside another namespace. Use of the dot(.) operator is required in order to access members of the nested namespace. Within the collections namespace like System, for instance, Generic is the nested namespace.
System.Collections.Generic
namespace name_of_namespace_1
{
// Member declarations & definitions
namespace name_of_namespace_2
{
// Member declarations & definitions
.
.
}
}
EXAMPLE CODE:
Let us take an example to illustrate how to access the namespace using nested namespace function in C#.
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:
OuterClass method in OuterNamespace
InnerClass method in InnerNamespace
Conclusion:
In conclusion, namespace access is made flexible and controllable in C# with a variety of mechanisms for naming standards and code organisation. Importing namespaces and more easily utilising their types is made possible via the most popular method-the 'using' directive. Alternatively, fully qualified names spare naming issues by giving explicit access to types. For lengthy or frequently used namespaces, alias directives make it possible to create personalised shortcuts for them. In situations when there are name conflicts, the 'global' modifier guarantees clear references. Nested namespaces provide a hierarchical framework to further organise similar functionality within larger namespaces. This highlights the significance of clear and maintainable code practices in C# and helps determine which approach is best based on the particular requirements of the codebase.