<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

      [摘要]Whidbey 初體驗 之 局部類型 ( partial 類型) Visual Studio 2005 [Whidbey] 搶先體驗版 [Express Beta 1 ] 出來有一段時間了,并且在微...
      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:tiy@citiz.net
      bbs:www.shixm.com/bbs



      日韩精品一区二区三区高清