Step by Step C # language [namespace]

Source: Internet
Author: User
  • Basic concepts of namespaces

    1. It is a method that provides application code containers in. net. It can uniquely identify the code and its content.

    2. Used as a way to classify items in. NET Framework.



  • Namespace Definition

    Format: namespace <namespace name> {}

    1. Use the namespace keyword to explicitly define the namespace for the code block in curly brackets.

    2. By default, the C # code is included in the global namespace.

    3. If you use a namespace name outside the namespace, you must enter a qualified name for the namespace.

    4. The specified name contains all the layer information of the namespace.

    5. specify the period (.) for different namespaces (.).

    6. If the code in a namespace needs to use the name defined in another namespace, it must include references to the name.



  • Namespace example


  • // eg. 1namespace LevelOne{    // code in LevelOne namespace     // name "NameOne" defined    // ==> reference NameOne: NameOne}// code in Global namespace// ==> reference NameOne: LevelOne.NameOne// eg. 2namespace LevelOne{    // code in LevelOne namespace    // ==> reference NameTwo: LevelTwo.NameTwo    namespace LevelTwo    {        // code in LevelOne.LevelTwo namespace        // name "NameTwo" defined        // ==> reference NameTwo: NameTwo    }}// code in Global namespace// ==> reference NameTwo: LevelOne.LevelTwo.NameTwo// eg. 3namespace LevelOne{    // code in LevelOne namespace    // name "NameThree" defined    // ==> 1. reference NameThree: LevelTwo.NameThree    // ==> 2. reference NameThree: NameThree(LevelOne.NameThree)    namespace LevelTwo    {        // code in LevelOne.LevelTwo namespace        // name "NameThree" defined        // ==> reference NameThree: NameThree    }}// code in Global namespace// ==> 1. reference NameThree: LevelOne.LevelTwo.NameThree// ==> 2. reference NameThree: LevelOne.NameThree// eg. 4namespace LevelOne{    // code in LevelOne namespace    using LevelTwo    // ==> reference NameTwo: NameTwo or LevelTwo.NameTwo    namespace LevelTwo    {        // code in LevelOne.LevelTwo namespace        // name "NameTwo" defined    }}// eg.5namespace LevelOne{    // code in LevelOne namespace    // name "NameTwo" defined    using LevelTwo    // ==> reference NameTwo: LevelTwo.NameTwo    // ==> reference NameTwo: NameTwo is error!    namespace LevelTwo    {        // code in LevelOne.LevelTwo namespace        // name "NameTwo" defined    }}// eg.6namespace LevelOne{    // code in LevelOne namespace    // name "NameThree" defined    using LT = LevelTwo;    // ==> reference NameThree: LT.NameThree    // ==> reference NameThree: NameThree(LevelOne.NameThree)    namespace LevelTwo    {        // code in LevelOne.LevelTwo namespace        // name "NameThree" defined    }}


Note: 1) In a namespace, you can use the namespace keyword to define a nested namespace. The nested namespace is referenced by its layer-level structure, and the hierarchy is distinguished by periods.

2) The name is uniquely defined by the namespace.

3) use the using keyword to access the namespace.

Example: using system;

4) use the using keyword to define the namespace alias.

Example: Using newnamespace = oldnamespace;

5) The using statement can be applied to the namespace containing them and the nested namespace contained in the namespace.

Example: Using levelone. leveltwo;

6) The using statement itself cannot access the name in another namespace. Unless the code in the namespace is linked to the project in some way, the code is defined in the source file of the project, or defined in other code linked to the project, otherwise, you cannot access the name contained in it.

This article from the "action is better than words" blog, please be sure to keep this source http://vikxiao.blog.51cto.com/9189404/1551227

Step by Step C # language [namespace]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.