Namespaces called namespace are a way of organizing the different types involved in a C # program.
In this tutorial, we will learn about Namespaces, how to define it, access its members, and use it in a C# program with examples.
We can compare the namespace to folders, the behavior is identical to a folder, it is possible to have elements of the same name in different namespace.
The entire framework.net is based on namespaces, it allows above all a good organization and facilitates the search of the elements.
Example, everything about collections will be in System.Collections. All about disk access is in System.IO.
When designing your program, it is strongly recommended to create your own namespace.
Create a Namespace
To create your namespace, it is sufficient before each class to specify in which namespace is your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace VehicleMotorise { class Car { } class Truck { } } namespace VehicleNoMotorise { class Velo { } } |
Use your namespace
To use your namespace, it works like those of the framework. Example with the namespace created above.
1 2 3 4 |
VehicleMotorise.Car v = new MotorMotor.Car (); VehicleNoMotorise.Velo = new VehicleNoMotorise.Velo (); |
Some namespace are very long, to avoid having to write them in full, you can at the beginning of your file in the zone of declaration of the namespace to add some. To add the namespace shortcuts:
1 2 3 |
Using VehicleNonMotorise; |
This allows you in your code to use the Car and Bike classes directly without specifying the namespace before.
Examples 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace MyNamespace { public class SampleClass { public static void myMethod() { Console.WriteLine("Creating my namespace"); } } } namespace MyProgram { public class MyClass { public static void Main() { MyNamespace.SampleClass.myMethod(); } } } |
When we run the program, the output will be:
1 2 3 |
<samp>Creating my namespace</samp> |
In the above program, we have created our own namespace MyNamespace
and accessed its members from Main()
method inside MyClass
. As said earlier, the dot (.)
operator is used to access the member of namespace.
In the Main()
method, myMethod()
method is called using the dot (.)
operator.
Using a Namespace in C# [The using Keyword]
A namespace can be included in a program using the using keyword. The syntax is,
1 2 3 |
using Namespace-Name; |
For example,
1 2 3 |
using System; |
The advantage of this approach is we don’t have to specify the fully qualified name of the members of that namespace every time we are accessing it.
Once the line
1 2 3 |
using System; |
is included at the top of the program. We can write
1 2 3 |
Console.WriteLine("Hello World!"); |
Instead of the fully qualified name i.e.
1 2 3 |
System.Console.WriteLine("Hello World!"); |
Nested Namespace in C#
A namespace can contain another namespace. It is called nested namespace. The nested namespace and its members can also be accessed using the dot (.)
operator.
The syntax for creating nested namespace is as follows:
1 2 3 4 5 6 7 8 9 |
namespace MyNamespace { namespace NestedNamespace { // Body of nested namespace } } |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; // Nested Namespace namespace MyNamespace { namespace Nested { public class SampleClass { public static void myMethod() { Console.WriteLine("Nested Namespace Example"); } } } } namespace MyProgram { public class MyClass { public static void Main() { MyNamespace.Nested.SampleClass.myMethod(); } } } |
When we run the program, the output will be:
1 2 3 |
<samp>Nested Namespace Example</samp> |
This example illustrates how nested namespace can be implemented in C#.
Here, we now have an extra namespace inside MyNamespace
called Nested
. So, instead of using MyNamespace.SampleClass.myMethod()
, we have to use MyNamespace.Nested.SampleClass.myMethod()
.