天天看點

Whidbey 初體驗之局部類型 ( partial 類型)

Whidbey 初體驗 之 局部類型 ( partial 類型) 

Visual Studio 2005 [Whidbey] 搶先體驗版 [Express Beta 1 ] 出來有一段時間了,并且在微軟的官方網站上有免費的下載下傳(下載下傳位址:http://lab.msdn.microsoft.com/vs2005/)。就本人而言是非常喜歡c#這一新生的語言的。也許并不能說它是新生的,它是對以往各種語言的提煉,或許它是站在巨人的肩膀上的,是以才顯得如此的優秀。伴随體驗版而來的c# 2.0 給我們帶來了新的語言特性(generics:泛型; iterators:疊代; partial classes:局部類型; anonymous methods:匿名方法;),使我們能更容易的編寫出簡潔明快的代碼,當然這些新特性給我們帶來的遠不止簡潔明快的代碼。這隻有在我們使用的過程中自己體會和别人的交流中了解。

分别用2003和2005 建立兩個WindowsApplication1

2003和2005解決方案資料總管中都會預設建立一個從System.Windows.Forms.Form 類繼承的窗體類Form1

那我們比較下兩個不同的IDE環境為我們自動生成的Form1的代碼是怎麼樣的。

選中Form1.cs察看代碼

2003:

public class Form1 : System.Windows.Forms.Form

 {

  private System.Windows.Forms.Button button1;

  /// <summary>

  /// 必需的設計器變量。

  /// </summary>

  private System.ComponentModel.Container components = null;

  public Form1()

  {

   //

   // Windows 窗體設計器支援所必需的

   //

   InitializeComponent();

   //

   // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼

   //

  }

  /// <summary>

  /// 清理所有正在使用的資源。

  /// </summary>

  protected override void Dispose( bool disposing )

  {

   if( disposing )

   {

    if (components != null)

    {

     components.Dispose();

    }

   }

   base.Dispose( disposing );

  }

  #region Windows 窗體設計器生成的代碼

  /// <summary>

  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

  /// 此方法的内容。

  /// </summary>

  private void InitializeComponent()

  {

   this.button1 = new System.Windows.Forms.Button();

   this.SuspendLayout();

   //

   // button1

   //

   this.button1.Location = new System.Drawing.Point(88, 72);

   this.button1.Name = "button1";

   this.button1.Size = new System.Drawing.Size(72, 32);

   this.button1.TabIndex = 0;

   this.button1.Text = "button1";

   this.button1.Click += new System.EventHandler(this.button1_Click);

   //

   // Form1

   //

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

   this.ClientSize = new System.Drawing.Size(292, 273);

   this.Controls.Add(this.button1);

   this.Name = "Form1";

   this.Text = "Form1";

   this.ResumeLayout(false);

  }

  #endregion

  /// <summary>

  /// 應用程式的主入口點。

  /// </summary>

  [STAThread]

  static void Main()

  {

   Application.Run(new Form1());

  }

  private void button1_Click(object sender, System.EventArgs e)

  {

  }

 }

2005:

partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

        }

    }

察看兩個環境下Form1的代碼檔案 Form1.cs檔案裡對Form1的代碼差别很大,2005中隻有那麼一點點,對button1的定義沒有,Click事件委托也沒有隻有一個button1_Click()顯然是有問題的。如果而且我們很快發現Class Form1是被定義成 partial 的也就是C# 2.0種的新的語言特征 局部類型。然後我們再點一下2005 IDE 解決方案資料總管上的Show All Files按鈕,會發現Form1.cs下多了個檔案 Form1.Designer.cs 這是2003環境下是沒有的, 察看該檔案我們會發現對Class Form1的另一部份定義。

 partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows Form Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.SuspendLayout();

//

// button1

//

            this.button1.Location = new System.Drawing.Point(75, 49);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(96, 46);

            this.button1.TabIndex = 0;

            this.button1.Text = "button1";

            this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

            this.ClientSize = new System.Drawing.Size(292, 273);

            this.Controls.Add(this.button1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;

    }

現在好像2005對Form1的描述好像全了,2005中Form1.cs 和 Form1.Designer.cs 兩個檔案中對Class Form1的描述相加 就是 2003 Form1.cs 中對Class Form1的描述。由此看來 partial 類型可以使我們把對某個類的描述寫在不同地方,甚至寫到兩個或多個不同的檔案中去。partial 資訊隻對編譯器有用,編譯器在編譯時看到對某個類的描述是“碎”的(partial 的),它會去其他地方收集該類的其他碎片,然後把所有的該類的碎片組合成完整的一個類,再對其編譯。是以partial 展現不到編譯好的 IL中去的。至于partial類型給我們帶來怎麼樣的意義呢?我們以後再讨論。

#結束

qq:14754875

email:[email protected]

bbs:www.shixm.com/bbs

繼續閱讀