Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Namespaces

C# Namespaces

NamespacingIt is designed to provide a way to keep a set of names separate from other names. The name of a class declared in one namespace does not conflict with the name of the same class declared in another namespace.

Let's give an example in a computer system: a folder (directory) can contain multiple subfolders; each subfolder cannot have the same file name, but files in different subfolders can have the same name.

Defining a namespace

A namespace is defined with the keyword namespace followed by the namespace name, as shown below:

namespace

namespace_name

{

   

// Code declaration

}

To call a function or variable that supports a namespace version, the namespace name is placed in front, as shown below:



namespace_name.item_name;

The following program demonstrates the use of namespaces:

Example

using

System

;

namespace

first_space

{

   

class

namespace_cl

   

{

     

public

void

func

(

)

     

{

         Console

.

WriteLine

(

"Inside first_space"

)

;

     

}

   

}

}

namespace

second_space

{

   

class

namespace_cl

   

{

     

public

void

func

(

)

     

{

         Console

.

WriteLine

(

"Inside second_space"

)

;

     

}

   

}

}

 

class

TestClass

{

   

static

void

Main

(

string

[

]

args

)

   

{

      first_space

.

namespace_cl

fc

=

new

first_space

.

namespace_cl

(

)

;

      second_space

.

namespace_cl

sc

=

new

second_space

.

namespace_cl

(

)

;

      fc

.

func

(

)

;

      sc

.

func

(

)

;

      Console

.

ReadKey

(

)

;

   

}

}

When the code above is compiled and executed, it produces the following result:



Inside first_space

Inside second_space

using The keyword

using keyword indicates that the program uses names from the given namespace. For example, we use the System namespace in our program, which defines the class Console. We can simply write:



Console.WriteLine ("Hello there");

We can write the fully qualified name, as follows:



System.Console.WriteLine("Hello there");

You can also use using namespace directive, so that you do not need to prefix the namespace name when using it. This directive tells the compiler that subsequent code uses names from the specified namespace. The following code demonstrates the application of namespaces.

Let's rewrite the above example using the using directive:

Example

using

System

;

using

first_space

;

using

second_space

;

namespace

first_space

{

   

class

abc

   

{

     

public

void

func

(

)

     

{

         Console

.

WriteLine

(

"Inside first_space"

)

;

     

}

   

}

}

namespace

second_space

{

   

class

efg

   

{

     

public

void

func

(

)

     

{

         Console

.

WriteLine

(

"Inside second_space"

)

;

     

}

   

}

}

 

class

TestClass

{

   

static

void

Main

(

string

[

]

args

)

   

{

      abc fc

=

new

abc

(

)

;

      efg sc

=

new

efg

(

)

;

      fc

.

func

(

)

;

      sc

.

func

(

)

;

      Console

.

ReadKey

(

)

;

   

}

}

When the code above is compiled and executed, it produces the following result:



Inside first_space

Inside second_space

Nested namespaces

Namespaces can be nested, that is, you can define one namespace inside another, as shown below:



namespace namespace_name1 

{

   // Code declaration

   namespace namespace_name2 

   {

     // Code declaration

   }

}

You can use the dot (.) operator to access members of a nested namespace, as shown below:

Example

using

System

;

using

SomeNameSpace

;

using

SomeNameSpace.Nested

;

namespace

SomeNameSpace

{

   

public

class

MyClass

   

{

       

static

void

Main

(

)

       

{

            Console

.

WriteLine

(

"In SomeNameSpace"

)

;

            Nested

.

NestedNameSpaceClass

.

SayHello

(

)

;

       

}

   

}

   

// Nested namespace

   

namespace

Nested  

   

{

       

public

class

NestedNameSpaceClass

       

{

           

public

static

void

SayHello

(

)

           

{

                Console

.

WriteLine

(

"In Nested"

)

;

           

}

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



In SomeNameSpace

In Nested