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
The using keyword indicates that the program is using given namespace names. For 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.
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 (); } }
The result after running the code will be the same as the above 2 codes.
According to Tutorialspoint
Previous article: Interface in C #
Next article: Preprocessing directive in C #