Namespace in C #

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 following example 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. 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.

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 (); } }

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 #

4 ★ | 1 Vote

May be interested

  • Things you didn't know about 26 types of Pokéball - Part 1Things you didn't know about 26 types of Pokéball - Part 1
    pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.