Summary: in this tutorial, you’ll learn about C# namespaces and how to use namespaces to organize code.
Introduction to the C# namespaces
C# namespaces allow you to group related classes, interfaces, structs, enums, and delegates into a single logical unit. Namespaces also help you avoid naming conflict issues.
To declare a namespace, you use the namespace
keyword followed by the namespace name as follows:
namespace namespaceName
{
// code elements
}
Code language: C# (cs)
For example, you can create the HR.cs
file and define a new namespace called
:HR
namespace HR
{
// code elements
}
Code language: C# (cs)
In this example,
is the name of the namespace. Inside the HR
namespace, you can define types like classes and interfaces. For instance:HR
namespace HR
{
class Employee
{
public string Name
{
get; set;
}
public string JobTitle
{
get; set;
}
public override string ToString() => $"{Name} ({JobTitle})";
}
}
Code language: C# (cs)
All the types that you define inside the curly braces of the
namespace will belong to the HR
namespace.HR
Starting from C# 10, you can simplify the namespace syntax by removing the opening and closing curly braces like this:
namespace HR;
class Employee
{
public string Name
{
get; set;
}
public string JobTitle
{
get; set;
}
public override string ToString() => $"{Name} ({JobTitle})";
}
Code language: C# (cs)
By doing this, all the types that you define in the HR.cs
file will belong to the
namespace.HR
This new syntax looks cleaner and saves horizontal space. It also makes your code easier to read.
To use the Employee
class inside the Program.cs
file, you can reference the fully qualified name of the class like this:
var employee = new HR.Employee()
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee);
Code language: C# (cs)
Typically, you use the using
statement to reference a class in a namespace without specifying the fully qualified namespace of that class. For example:
using HR;
var employee = new Employee()
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee);
Code language: C# (cs)
In this example, we use the using statement to explicitly import the HR
namespace and reference the Employee
class using just the class name only.
Nested namespaces
Nested namespaces are namespaces that you define in another namespace. They allow you to organize the code elements more granularly especially when you work with larger codebases.
To define nested namespaces in C#, you simply declare them inside another namespace. For example:
namespace HR
{
namespace Personnel
{
class Employee
{
public string Name
{
get; set;
}
public string JobTitle
{
get; set;
}
public override string ToString() => $"{Name} ({JobTitle})";
}
}
namespace Payroll
{
class SalaryCalculator
{
}
}
}
Code language: C# (cs)
In this example, we define two nested namespaces Personnel
and Payroll
inside the HR
namespace. The Personnel
namespace has the Employee
class and the Payroll
namespace has the SalaryCalculator
class.
To reference a nested namespace, you use dot notation to indicate the parent-child relationship between the namespaces. For example:
using HR.Personnel;
using HR.Payroll;
Code language: C# (cs)
In this example, we use the using
statement to import the Personnel
and Payroll
namespaces from the HR
namespace.
And you can reference the types in these nested namespaces without the fully qualified names:
using HR.Personnel;
using HR.Payroll;
var employee = new Employee()
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee);
var salaryCalculator = new SalaryCalculator();
Code language: C# (cs)
When you use nested namespaces, it’s important to keep the nesting level at a reasonable level. If you nest too deeply, your code will be harder to understand and maintain. It’s a common practice to aim for a nesting level of no more than 3 or 4 levels deep.
Notice that when you import a namespace, it doesn’t automatically import the nested namespace. For example:
using HR;
var employee = new Employee() // ERROR
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee);
Code language: C# (cs)
In this example, the using
statement only imports the types in the HR
namespace but types in the nested namespace. Therefore, it results in an error.HR
To fix it, you need to import the HR
.Personnel namespace or reference the Employee
class using a fully qualified name.
Using C# namespace to avoid naming conflict
Namespaces can help you avoid naming conflicts. For example, you may have the same class name in different namespaces.
This is because namespaces act as a container for code elements and when you prefix the code elements with namespaces, the code elements will be uniquely identifiable.
For example:
namespace HR
{
namespace Dependents
{
class Person
{
public string Name
{
get; set;
}
}
}
namespace Personnel
{
class Person
{
public string Name
{
get; set;
}
}
class Employee : Person
{
public string JobTitle
{
get; set;
}
public override string ToString() => $"{Name} ({JobTitle})";
}
}
}
Code language: C# (cs)
In this example, we have the same Person class in different namespaces Dependents
and Personnel
. If you import both HR.Personnel
and HR.Dependents
namespaces into the same file, you’ll get an error:
using HR.Personnel;
using HR.Dependents;
var child = new Person(); // ERROR
Code language: C# (cs)
The reason is that when you reference the Person
class, the compiler doesn’t know the exact namespace of the Person
class.
To fix this, you need to use a fully qualified class name and only import the namespace that you use often in the code. For example:
using HR.Dependents;
// comes from HR.Dependents
var child = new Person();
// comes from HR.Personnel
var father = new HR.Personnel.Employee();
Code language: C# (cs)
In this example, the Person
class refers to the Person class in the HR.Dependents
namespace. To reference the Employee
class in the HR.Personnel
namespace, you need to use the fully qualified class name.
C# namespace best practices
The following are the best practices when it comes to using namespaces in C#:
- Use meaningful and descriptive namespace names.
- Use nested namespaces to further organize code elements but avoid using nested namespaces that are too deep or complex.
- Use using directives sparingly and only for frequently used namespaces.
Summary
- Use namespace and nested namespace to make your code more organized, especially when working with a large codebase.
- Use namespaces to avoid naming conflicts.