Clear, practical technology insights About · Contact

Namespace in C #: Tips and Examples

Understand Namespace in C # with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and common mistakes.

Published: 5 minutes read
Table of Contents

Namespace in C # is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

In C # , namespaces are designed to keep a set of distinct names separated. The same class names that are declared in different namespaces do not conflict with each other.

Defining a Namespace in C #

A C # namespace definition starts with the namespace keyword followed by the namespace name, as follows:

 namespace t ê n_namespace { //phần khai báo code } 

To call the enabled version of the function's namespace or variable, you add it after the namespace name as follows:

 t ê n_namespace . t ê n_ph ầ n_t ử; 

the example below illustrates the usage of namespaces in C #:

 using System ; namespace namespaceA { class namespace_l { public void InNamespace () { Console . WriteLine ( "Đây là namespaceA" ); } } } namespace namespaceB { class namespace_l { public void InNamespace () { Console . WriteLine ( "Đây là namespaceB " ); } } } class Tester { static void Main ( string [] args ) { namespaceA . namespace_l ns1 = new namespaceA . namespace_l (); namespaceB . namespace_l ns2 = new namespaceB . namespace_l ();
 Console.WriteLine ("Học C# cơ bản trên TipsMake.com.");
 Console.WriteLine ("Ví dụ về Namespace trong C#:");
 Console.WriteLine ("-----------------------------------"); ns1 . InNamespace (); ns2 . InNamespace (); Console . ReadKey (); } } 

Running the above program we will have the following result:

 Learn basic C # on TipsMake.com. 
 Example of Namespace in C #: 
 ----------------------------------- 
 This is namespaceA 
 This is namespaceB 

Using keyword in C #

The using keyword indicates that the program is using given namespace names. As an example,, we use System namespaces in programs. The Console class is defined here. We write:

 Console.WriteLine ("Category C # TipsMake.com."); 

Or you can write your full name:

 System.Console.WriteLine ("Category C # TipsMake.com."); 

Using using, you will avoid having to add namespaces before the class name. Using this tells the compiler that the next code is using names in the defined namespace.

Now rewrite the example above using using directive in C #:

 using System ;
 using namespaceA;
using namespaceB; namespace namespaceA { class namespace_l { public void InNamespace () { Console . WriteLine ( "Đây là namespaceA" ); } } } namespace namespaceB { class namespace_2 { public void InNamespace () { Console . WriteLine ( "Đây là namespaceB " ); } } } class Tester { static void Main ( string [] args ) { namespaceA . namespace_l ns1 = new namespaceA . namespace_l (); namespaceB . namespace_2 ns2 = new namespaceB . namespace_2 ();
 Console.WriteLine ("Học C# cơ bản trên TipsMake.com.");
 Console.WriteLine ("Ví dụ về Namespace trong C#:");
 Console.WriteLine ("-----------------------------------"); ns1 . InNamespace (); ns2 . InNamespace (); Console . ReadKey (); } } 

The results of running this program remain the same, except that you do not have to add namespace names to the class names.

Nested Namespace in C #

In C #, you can define a namespace within other namespaces, as follows:

 namespace name_namespace_1 
 { 
 // code declaration section 
 namespace name_namespace_2 
 { 
 // code declaration section 
 } 
 } 

You can access the members of these nested namespaces by using the dot (.) Operator in C #, as follows:

 using System ;
 using namespaceA;
using namespaceA.namespaceB; namespace namespaceA { class namespace_l { public void InNamespace () { Console . WriteLine ( "Đây là namespaceA" ); } } namespace namespaceB { class namespace_2 { public void InNamespace () { Console . WriteLine ( "Đây là namespaceB " ); } }
 } } class Tester { static void Main ( string [] args ) { namespace_l ns1 = new namespace_l (); namespace_2 ns2 = new namespace_2 ();
 Console.WriteLine ("Học C# cơ bản trên TipsMake.com.");
 Console.WriteLine ("Ví dụ về Namespace trong C#:");
 Console.WriteLine ("-----------------------------------"); ns1 . InNamespace (); ns2 . InNamespace (); Console . ReadKey (); } } 

Frequently Asked Questions

What should you know about defining a Namespace in C #?

A C # namespace definition starts with the namespace keyword followed by the namespace name, as follows:

What should you know about using keyword in C #?

The using keyword indicates that the program is using given namespace names. As an example,, we use System namespaces in programs. The Console class is defined here. We write:

What should you know about nested Namespace in C #?

In C #, you can define a namespace within other namespaces, as follows:

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.