Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are nested namespaces in C#?
A nested namespace in C# is a namespace declared inside another namespace. This hierarchical structure helps organize code logically by grouping related classes and functionalities under appropriate namespace levels.
Nested namespaces provide better code organization, prevent naming conflicts, and create a logical hierarchy that reflects the structure of your application.
Syntax
Following is the syntax for declaring nested namespaces −
namespace OuterNamespace {
namespace InnerNamespace {
public class MyClass {
// class members
}
}
}
You can also use the dot notation to declare nested namespaces −
namespace OuterNamespace.InnerNamespace {
public class MyClass {
// class members
}
}
How Nested Namespaces Work
Using Basic Nested Namespaces
Example
using System;
namespace Outer {
class Program {
static void Main(string[] args) {
Inner.InnerClass cls = new Inner.InnerClass();
Console.WriteLine("Welcome!");
DisplayOuter();
cls.DisplayInner();
}
public static void DisplayOuter() {
Console.WriteLine("Outer Namespace");
}
}
namespace Inner {
public class InnerClass {
public void DisplayInner() {
Console.WriteLine("Inner Namespace");
}
}
}
}
The output of the above code is −
Welcome! Outer Namespace Inner Namespace
Using Dot Notation for Nested Namespaces
Example
using System;
namespace Company.HR.Employee {
public class EmployeeInfo {
public void ShowInfo() {
Console.WriteLine("Employee Information System");
}
}
}
namespace Company.Finance.Accounting {
public class AccountManager {
public void ProcessPayroll() {
Console.WriteLine("Processing Payroll");
}
}
}
class Program {
static void Main(string[] args) {
Company.HR.Employee.EmployeeInfo emp = new Company.HR.Employee.EmployeeInfo();
Company.Finance.Accounting.AccountManager acc = new Company.Finance.Accounting.AccountManager();
emp.ShowInfo();
acc.ProcessPayroll();
}
}
The output of the above code is −
Employee Information System Processing Payroll
Using Aliases with Nested Namespaces
You can create aliases for long nested namespace paths to make your code more readable −
Example
using System;
using EmpInfo = Company.HR.Employee;
using AccMgr = Company.Finance.Accounting;
namespace Company.HR.Employee {
public class Staff {
public void DisplayStaffInfo() {
Console.WriteLine("Staff member details");
}
}
}
namespace Company.Finance.Accounting {
public class Budget {
public void ShowBudget() {
Console.WriteLine("Annual budget report");
}
}
}
class Program {
static void Main(string[] args) {
EmpInfo.Staff staff = new EmpInfo.Staff();
AccMgr.Budget budget = new AccMgr.Budget();
staff.DisplayStaffInfo();
budget.ShowBudget();
}
}
The output of the above code is −
Staff member details Annual budget report
Benefits of Nested Namespaces
| Benefit | Description |
|---|---|
| Organization | Groups related classes logically in a hierarchical structure |
| Naming Conflicts | Prevents class name collisions by providing unique namespace paths |
| Modularity | Enables better separation of concerns and modular design |
| Readability | Makes code structure more intuitive and easier to navigate |
Conclusion
Nested namespaces in C# provide a hierarchical way to organize code by placing namespaces inside other namespaces. They help prevent naming conflicts, improve code organization, and create logical groupings that reflect your application's structure. Use dot notation or nested declarations to create multi-level namespace hierarchies.
