Introduction
Namespaces in C# are used to organize and provide a level of separation for classes, interfaces, structs, enums, and delegates. They help avoid naming conflicts by grouping logically related types under a single name. Namespaces also make it easier to locate and manage the types in a large project.
Defining a Namespace
A namespace is defined using the namespace
keyword followed by the namespace name. The types declared within the namespace are encapsulated in it.
Syntax
namespace NamespaceName
{
// Types declared within the namespace
class MyClass
{
// Class members
}
}
Example
namespace MyApplication
{
public class MyClass
{
public void DisplayMessage()
{
Console.WriteLine("Hello from MyClass in MyApplication namespace!");
}
}
}
Using Namespaces
To use the types declared in a namespace, you can either use the fully qualified name of the type or add a using
directive at the top of your file.
Using the Fully Qualified Name
class Program
{
static void Main(string[] args)
{
MyApplication.MyClass myClass = new MyApplication.MyClass();
myClass.DisplayMessage();
}
}
Using the using
Directive
using MyApplication;
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.DisplayMessage();
}
}
Nested Namespaces
Namespaces can be nested within other namespaces to create a hierarchical structure.
Example
namespace MyApplication
{
namespace Utilities
{
public class UtilityClass
{
public void DisplayUtilityMessage()
{
Console.WriteLine("Hello from UtilityClass in MyApplication.Utilities namespace!");
}
}
}
}
Using Nested Namespaces
using MyApplication.Utilities;
class Program
{
static void Main(string[] args)
{
UtilityClass utilityClass = new UtilityClass();
utilityClass.DisplayUtilityMessage();
}
}
Namespace Aliases
Namespace aliases allow you to create an alias for a namespace, which can make your code easier to read, especially when dealing with long namespace names or resolving naming conflicts.
Example
using Util = MyApplication.Utilities;
class Program
{
static void Main(string[] args)
{
Util.UtilityClass utilityClass = new Util.UtilityClass();
utilityClass.DisplayUtilityMessage();
}
}
The global Namespace
The global
keyword is used to reference the global namespace, which is the default namespace for all C# programs. This is useful to avoid naming conflicts when you have types with the same name in different namespaces.
Example
namespace MyApplication
{
public class MyClass
{
public void DisplayMessage()
{
Console.WriteLine("Hello from MyClass in MyApplication namespace!");
}
}
}
class Program
{
static void Main(string[] args)
{
global::MyApplication.MyClass myClass = new global::MyApplication.MyClass();
myClass.DisplayMessage();
}
}
Practical Example
Let’s create a practical example where we use namespaces to organize a small project.
File: Program.cs
using System;
using MyApplication.Services;
using MyApplication.Models;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
UserService userService = new UserService();
User user = new User { Name = "John Doe", Age = 30 };
userService.DisplayUser(user);
}
}
}
File: Models/User.cs
namespace MyApplication.Models
{
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
}
File: Services/UserService.cs
using System;
using MyApplication.Models;
namespace MyApplication.Services
{
public class UserService
{
public void DisplayUser(User user)
{
Console.WriteLine($"Name: {user.Name}, Age: {user.Age}");
}
}
}
Conclusion
Namespaces in C# are a powerful way to organize and manage types in your applications. They help avoid naming conflicts and make your code more modular and maintainable. By using namespaces effectively, you can create well-structured and easy-to-navigate codebases.