Namespace in C# is used in a program to organize the code separately from each other. Classes are maintained with namespace to organize in a better way.
With the use of namespace, we can keep one set of names different from another set of names. The vital importance of namespace is that it prevents the collision of the same class names declared in two different namespaces.
A namespace can have Namespaces (Nested Namespace), Classes, Interfaces, Structures, and Delegates.
Defining a Namespace
The keyword “namespace” is used to define the namespace and the namespace can be nested.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 | namespace name_of_namespace { // Namespace (Nested Namespaces) // Classes // Interfaces . . . . . . . . . . . } |
Sample example of the namespace:
1 2 3 4 5 6 7 8 9 10 | namespace NamespaceEg { class ClassEg { public void method_1() { System.Console.WriteLine("Namespace created"); } } } |
Accessing members of namespace in C#
The members of the namespace can be accessed using dot(.)
operator. The syntax for accessing namespace members is as followed.
namespace_name.member_name
Remember that, two classes inside the same namespace cannot have the same name but the class from a different namespace may have the same class name.
Example 1: C# program to demostrate Namespace
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 30 31 32 33 34 | using System; namespace first_ns { class ClassGreet { public void func() { Console.WriteLine("This is First Namespace"); } } } namespace second_ns { class ClassGreet { public void func() { Console.WriteLine("This is Second Namespace"); } } } class TestClass { static void Main(string[] args) { first_ns.ClassGreet f_ns = new first_ns.ClassGreet(); second_ns.ClassGreet s_ns = new second_ns.ClassGreet(); f_ns.func(); s_ns.func(); } } |
Output:
This is First Namespace
This is Second Namespace
As you can see in the above program, the name of the classes are the same inside the two different namespaces and still, the program did not get affected. We created two different objects for two different namespaces classes and accessed the functions respectively.
The using Keyword
A namespace can be included in the program with the help of using keyword. We already use a name using keyword for System in a program.
Using System in a program we are able to write Console.WriteLine(Hello Simple2code.com")
instead of writing the whole System.Console.WriteLine(Hello
. We include it in the following in the program.Simple2co
de.com")
using namespace_name;
Let us go through the above program again and rewrite it using the using keyword.
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 30 31 32 33 34 35 36 37 | using System; using first_ns; using second_ns; namespace first_ns { class FirstGreet { public void func() { Console.WriteLine("This is First Namespace"); } } } namespace second_ns { class SecondGreet { public void func() { Console.WriteLine("This is Second Namespace"); } } } class TestClass { static void Main(string[] args) { FirstGreet f_ns = new FirstGreet(); SecondGreet s_ns = new SecondGreet(); f_ns.func(); s_ns.func(); } } |
Output:
This is First Namespace
This is Second Namespace
Nested Namespaces in C#
We can also nest the namespace in a program as already told above. It means inserting a namespace inside a namespace in a program and its members can also be accessed with the use of dot (.)
operator.
Syntax of the nested namespace:
1 2 3 4 5 6 7 | namespace outer_namespace { namespace inner_namespace { // Body of innner namespace } } |
Example: C# program for the nested namespace
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 Main_ns { namespace Nested_ns { class GreetClass { public void greet() { Console.WriteLine("Welcome to simple2code.com"); } } } } class MainClass { static void Main(string[] args) { Main_ns.Nested_ns.GreetClass f_ns = new Main_ns.Nested_ns.GreetClass(); f_ns.greet(); } } |
//OutputWelcome to simple2code.com
If you don’t want to write the whole statement of the main class to create an object then you can simply use using keyword to include the namespace in the following way.
Write the following at the beginning of the program.
1 2 | using Main_ns; using Main_ns.Nested_ns; |
And do the following inside the main function of the program.
1 2 3 4 5 6 7 8 | class MainClass { static void Main(string[] args) { GreetClass f_ns = new GreetClass(); f_ns.greet(); } } |
The output will be the same but you don’t have o repeat the long code again and again in the program.