A
namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.
Defining a Namespace
A namespace definition begins with the keyword
namespace followed by the namespace name as follows −
1namespace namespace_name {
2 // code declarations
3}
To call the namespace-enabled version of either function or variable, prepend the namespace name as follows −
1namespace_name.item_name;
The following program demonstrates use of namespaces −
1using System;
2
3namespace first_space {
4 class namespace_cl {
5 public void func() {
6 Console.WriteLine("Inside first_space");
7 }
8 }
9}
10namespace second_space {
11 class namespace_cl {
12 public void func() {
13 Console.WriteLine("Inside second_space");
14 }
15 }
16}
17class TestClass {
18 static void Main(string[] args) {
19 first_space.namespace_cl fc = new first_space.namespace_cl();
20 second_space.namespace_cl sc = new second_space.namespace_cl();
21 fc.func();
22 sc.func();
23 Console.ReadKey();
24 }
25}
When the above code is compiled and executed, it produces the following result −
1Inside first_space
2Inside second_space
The using Keyword
The
using keyword states that the program is using the names in the given namespace. For example, we are using the
System namespace in our programs. The class Console is defined there. We just write −
1Console.WriteLine ("Hello there");
We could have written the fully qualified name as −
1System.Console.WriteLine("Hello there");
You can also avoid prepending of namespaces with the
using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The namespace is thus implied for the following code −
Let us rewrite our preceding example, with using directive −
1using System;
2using first_space;
3using second_space;
4
5namespace first_space {
6 class abc {
7 public void func() {
8 Console.WriteLine("Inside first_space");
9 }
10 }
11}
12namespace second_space {
13 class efg {
14 public void func() {
15 Console.WriteLine("Inside second_space");
16 }
17 }
18}
19class TestClass {
20 static void Main(string[] args) {
21 abc fc = new abc();
22 efg sc = new efg();
23 fc.func();
24 sc.func();
25 Console.ReadKey();
26 }
27}
When the above code is compiled and executed, it produces the following result −
1Inside first_space
2Inside second_space
Nested Namespaces
You can define one namespace inside another namespace as follows −
1namespace namespace_name1 {
2
3 // code declarations
4 namespace namespace_name2 {
5 // code declarations
6 }
7}
You can access members of nested namespace by using the dot (.) operator as follows −
1using System;
2using first_space;
3using first_space.second_space;
4
5namespace first_space {
6 class abc {
7 public void func() {
8 Console.WriteLine("Inside first_space");
9 }
10 }
11 namespace second_space {
12 class efg {
13 public void func() {
14 Console.WriteLine("Inside second_space");
15 }
16 }
17 }
18}
19class TestClass {
20 static void Main(string[] args) {
21 abc fc = new abc();
22 efg sc = new efg();
23 fc.func();
24 sc.func();
25 Console.ReadKey();
26 }
27}
When the above code is compiled and executed, it produces the following result −
1Inside first_space
2Inside second_space