在宣告該命名空間的所屬專案內,皆能叫用該命名空間
只要透過using 關鍵字即可匯入命名空間,當然事先必須加入該命名空間所屬的參考
若要使用Microsoft.VisualBasic命名空間,就必須在專案中加入參考Micorsoft.VisualBasic.dll
命名空間中可包含:命名空間(namespace)、類別(class)、列舉(enum)、結構(structure)、委派(delegate)、介面(interface)
namespace namespace_ex
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_GetCarName_Click(object sender, EventArgs e)
        {
            Vehicle.Car.SportCar.Name = "Eclipse";  //注意,命名空間不需要new,即可使用
            MessageBox.Show(Vehicle.Car.SportCar.Name);
        }
    }
}

namespace Vehicle //交通工具,宣告命名空間
{

    namespace Car //汽車
    {
        /// <summary>
        /// 輪胎尺寸設定
        /// </summary>
        enum CarWheel
        {
            Eighteen = 18, Seventeen = 17, Sixteen = 16, Fifteen = 15
        }
        /// <summary>
        /// 汽車特性
        /// </summary>
        struct CarProfile
        {
            public string CarTechnology;
        }

        public class SportCar //跑車
        {   //設定輪胎尺寸為18吋
            public static int wheel = (int) CarWheel.Eighteen;

            private static string CarName;
            public static string Name
            {
                get
                {
                    if (CarName == "Eclipse")
                    {
                        return "三菱當家跑車-" + CarName;
                    }
                    else
                    {
                        return CarName;
                    }
                }
                set
                {
                    CarName = value;
                }

            }
            public static string Turbo(bool IsTurbo)
            {
                CarProfile CP;  //結構,還是得宣告為變數才能使用
                if (IsTurbo)
                  CP.CarTechnology = "渦輪增壓";
                else
                  CP.CarTechnology = "自然進氣";

                return CP.CarTechnology;
            }
        }
        public class Convertible //敞篷車
        {  }

        public class SUVs //Sport Utility Vehicles 休旅車
        {  }

    }
    namespace Train //火車
    { class MagLev { } } //磁浮火車
    namespace Airplane //飛機
    { class Airsuperiority{ } } //戰鬥機
}

命名空間別名(Alias)
using 別名 = 命名空間;

選擇命名空間的工具,注意,是在命名空間中使用
global::{}Microsoft
            {}namespaceAlias_ex
            {}System

arrow
arrow
    全站熱搜

    羅 朝淇 發表在 痞客邦 留言(0) 人氣()