瞭解C#命名空間

來源:互聯網
上載者:User
一、C#命名空間
    C#命名空間用來定義類的範的範圍,每個命名空間都包含可在程式中使用的類型:類,結構、枚舉、委託和介面。使用using引用命名空間,如常用的using System;
使用嵌套命名空間時,需要使用全路徑

二、使用命名空間的好處
     代碼可分佈在多個檔案中
     命名空間具有擴展性
     可以堆砌出層次式的類組織結構

三、代碼可分佈在多個檔案中
       相同的命名空間可以分布在不同的檔案中,如下面代碼中的CG命名空間分佈在namespace.cs和namespace01.cs兩個檔案中,這兩個檔案共同組合成一個命名空間CG
       註:嵌套類只能在同一檔案中,不可以跨不同檔案

namespace.cs檔案代碼:using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            PrintName pn=new PrintName();
            pn.intro();
        }
    }
}

namespace01.cs檔案代碼:using System;
namespace CG
{
    public class PrintName
    {
        public void intro()
        {
            Console.WriteLine("My name is A");
        }
    }
}

四、命名空間具有擴展性
    上例中兩個命名空間,分布在不同的檔案中,我們變更其中一個檔案而不會影響另一個檔案。這個命名空間更具有擴展性。如:將上例中副命名空間產產生Dll檔案,主命令空間引用此Dll。當副命名空單變更時,主命名空間不需要重新編譯,即可得到結果。
註:
4.1編輯主副檔案方法:    csc 主cs檔案 副cs檔案
如下命令:
    csc namespace.cs namespace01.cs

4.2產生類庫的方法:    csc /target:library  cs檔案(創建Dll檔案)
如下命令:
    csc /target:library namespace.cs

4.3主檔案與類庫關聯的方法    csc /reference:引用的類庫 主檔案名 
如下命令
    csc /reference:namespace01.dll namespace.cs

五、可以堆砌出層次式的類組織結構
    如下面代碼中的A.A1;A.A2兩個命名空單,這樣更容易分組與管理using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            A.A1.PrintName a=new A.A1.PrintName();
            a.intro();
            A.A2.PrintName b=new A.A2.PrintName();
            b.intro();
            Console.ReadLine();
        }
    }
}
namespace A
{
    namespace A1
    {
        public class PrintName
        {
            public void intro()
            {
                Console.WriteLine("My name is A");
            }
        }
    }
    namespace A2
    {
        public class PrintName
        {
            public void intro()
            {
                Console.WriteLine("My name is B");
            }
        }        
    }
}

六、命名空間別名(Alies)
    使用別名可以程式更簡捷
使用方法:
    using 別名=命名空間

如下面源代碼與變更後代碼對比:

源:using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            ParentNameSpace.ChildNameSpace.PrintName a=new ParentNameSpace.ChildNameSpace.PrintName();
            a.intro();
            Console.ReadLine();
        }
    }

}
namespace ParentNameSpace
{
    namespace ChildNameSpace
    {
        public class PrintName
        {
            public void intro()
            {
                Console.WriteLine("My name is A");
            }
        }
    }
}

此代碼變成別名形式為:

using System;
using MySpace=ParentNameSpace.ChildNameSpace;
namespace CG
{
    class test
    {
        static void Main()
        {
            MySpace.PrintName a=new MySpace.PrintName();
            a.intro();
            Console.ReadLine();
        }
    }

}
namespace ParentNameSpace
{
    namespace ChildNameSpace
    {
        public class PrintName
        {
            public void intro()
            {
                Console.WriteLine("My name is A");
            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.