The objective of this article is to understand the concept of the namespaces in C# programming language. As we know, day by day, the web and system applications are getting advanced in terms of multiple functionalities, and as a developer, it is a complex task to organize and maintain the classes and methods in larger projects. In C# programming, the concept namespaces help us to organize the various classes and methods to avoid conflicts.
The namespaces are primarily used to organize the various classes and methods in large projects. Since the beginning of this tutorial, we are using the Console.WiteLine()
statement to print output on a console. Also, we used the using System
directive at the top of every program. Here, System
is a namespace, and Console
is a class in the System namespace and to avoid writing the same namespace repeatedly like System.Console.WriteLine()
, we declared this at the top with the help of using
keyword.
Declaring a C# Namespace
A namespace is declared in C# using the namespace
keyword. The general syntax for a namespace and nested namespace is shown here:
namespace namespace_name
{
// member namespace
namespace namespace_name
{
// members
}
}
Note: The members declared in the namespace is in scope throughout the namespace. In a namespace, we can declare classes, structures, delegates, interfaces, or other namespaces.
Using C# Namespace: using
directive
Let's take an example of a namespace with the help of using
directive.
Filename: Program.cs
using System;
using YesBank;
namespace YesBank
{
public class Deposit
{
public void cash()
{
Console.WriteLine("Daily limit to deposit the cash is up to 30,000 INR");
}
}
}
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
// using the Deposit class defined in the YesBank namespace
Deposit d = new Deposit();
d.cash();
}
}
}
Output:
Daily limit to deposit the cash is up to 30,000 INR
In the code above firstly, we defined a separate namespace YesBank with the help of the namespace
keyword, which is outside of another namespace. Every time instead of writing the complete namespace, we declared that we are using the YesBank namespace with help of the using
directives.
At last, we created the object of Deposit class and called the cash()
method (Note: please refer to the following example where we didn't use the using
directive for user-defined namespaces).
Let's take another example of the namespaces where names are fully qualified (namespaces and types have unique titles)
Filename: Program.cs
using System;
namespace YesBank
{
public class Deposit
{
public void cash()
{
Console.WriteLine("Daily limit to deposit the cash is up to 30,000 INR");
}
}
}
namespace NoBank
{
public class Deposit
{
public void cash()
{
Console.WriteLine("Daily limit to deposit the cash is up to 40,000 INR");
}
}
}
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
// calling the class defined in a namespace using the namespace name
YesBank.Deposit d1 = new YesBank.Deposit();
d1.cash();
// calling the class defined in a namespace using the namespace name
NoBank.Deposit d2 = new NoBank.Deposit();
d2.cash();
}
}
}
Output:
Daily limit to deposit the cash is up to 30,000 INR
Daily limit to deposit the cash is up to 40,000 INR
In the above example, you might have noticed the same class and method names but in two different unique namespaces (both having different implementations). Hence, we called them as fully qualified names with namespaces else compiler could not resolve as the same class is present in two namespaces. Also, we didn't use the using
directive and thus while creating the object we used the specific namespaces followed by class name.
Let's take one more example, where namespaces are nested, and methods are static.
Filename: Program.cs
using System;
namespace Banks
{
// nested namespace
namespace YesBank
{
public class Deposit
{
public static void cash()
{
Console.WriteLine("Daily limit to deposit the cash is up to 30,000 INR");
}
}
}
// nested namespace
namespace NoBank
{
public class Deposit
{
public static void cash()
{
Console.WriteLine("Daily limit to deposit the cash is up to 40,000 INR");
}
}
}
public class Program
{
public static void Main(string[] args)
{
YesBank.Deposit.cash();
NoBank.Deposit.cash();
}
}
}
Output:
Daily limit to deposit the cash is up to 30,000 INR
Daily limit to deposit the cash is up to 40,000 INR
In this example, we have defined nested namespaces where the top hierarchy is Bank namespace, and inside of that, we have two different namespaces, YesBank and NoBank. As we know, static
methods keep only one copy of the method at the Type level, which means the last updated value of the method shared among all objects of that Type. Here we defined the methods as static
, but the class cash is present in two different namespaces. As the methods are static, hence we directly called the cash()
method using namespace followed by class name and method name.
Using Alias with C# namespace
Let's take one last example with a namespace alias.
Filename: Program.cs
// we can also provide a different name to namespace while using it
using Alias = System.Console;
public class Program
{
public static void Main(string[] args)
{
Alias.WriteLine("Hello, World!");
}
}
Output:
Hello, World!
The above example shows another form of using
directive. Here the we have provided a different, generally a simpler name for a namespace or class type using the using
directive and =
operator. Once the alias is created, it can be used throughout the program.
We hope this article helped you to understand the concept of namespaces in C# language. If you have any queries then, please let us know in the comment section. We are happy to solve your doubts.