Whidbey 初體驗(yàn)之局部類型 ( partial 類型)
2024-07-21 02:17:06
供稿:網(wǎng)友
 
 
whidbey 初體驗(yàn) 之 局部類型 ( partial 類型) 
visual studio 2005 [whidbey] 搶先體驗(yàn)版 [express beta 1 ] 出來有一段時(shí)間了,并且在微軟的官方網(wǎng)站上有免費(fèi)的下載(下載地址:http://lab.msdn.microsoft.com/vs2005/)。就本人而言是非常喜歡c#這一新生的語言的。也許并不能說它是新生的,它是對(duì)以往各種語言的提煉,或許它是站在巨人的肩膀上的,所以才顯得如此的優(yōu)秀。伴隨體驗(yàn)版而來的c# 2.0 給我們帶來了新的語言特性(generics:泛型; iterators:迭代; partial classes:局部類型; anonymous methods:匿名方法;),使我們能更容易的編寫出簡潔明快的代碼,當(dāng)然這些新特性給我們帶來的遠(yuǎn)不止簡潔明快的代碼。這只有在我們使用的過程中自己體會(huì)和別人的交流中了解。
分別用2003和2005 新建兩個(gè)windowsapplication1
2003和2005解決方案資源管理器中都會(huì)默認(rèn)建立一個(gè)從system.windows.forms.form 類繼承的窗體類form1
那我們比較下兩個(gè)不同的ide環(huán)境為我們自動(dòng)生成的form1的代碼是怎么樣的。
選中form1.cs察看代碼
2003:
public class form1 : system.windows.forms.form
 {
 private system.windows.forms.button button1;
 /// <summary>
 /// 必需的設(shè)計(jì)器變量。
 /// </summary>
 private system.componentmodel.container components = null;
 public form1()
 {
 //
 // windows 窗體設(shè)計(jì)器支持所必需的
 //
 initializecomponent();
 //
 // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
 //
 }
 /// <summary>
 /// 清理所有正在使用的資源。
 /// </summary>
 protected override void dispose( bool disposing )
 {
 if( disposing )
 {
 if (components != null) 
 {
 components.dispose();
 }
 }
 base.dispose( disposing );
 }
 #region windows 窗體設(shè)計(jì)器生成的代碼
 /// <summary>
 /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
 /// 此方法的內(nèi)容。
 /// </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>
 /// 應(yīng)用程序的主入口點(diǎn)。
 /// </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)
 {
 }
 }
察看兩個(gè)環(huán)境下form1的代碼文件 form1.cs文件里對(duì)form1的代碼差別很大,2005中只有那么一點(diǎn)點(diǎn),對(duì)button1的定義沒有,click事件委托也沒有只有一個(gè)button1_click()顯然是有問題的。如果而且我們很快發(fā)現(xiàn)class form1是被定義成 partial 的也就是c# 2.0種的新的語言特征 局部類型。然后我們?cè)冱c(diǎn)一下2005 ide 解決方案資源管理器上的show all files按鈕,會(huì)發(fā)現(xiàn)form1.cs下多了個(gè)文件 form1.designer.cs 這是2003環(huán)境下是沒有的, 察看該文件我們會(huì)發(fā)現(xiàn)對(duì)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;
 }
現(xiàn)在好像2005對(duì)form1的描述好像全了,2005中form1.cs 和 form1.designer.cs 兩個(gè)文件中對(duì)class form1的描述相加 就是 2003 form1.cs 中對(duì)class form1的描述。由此看來 partial 類型可以使我們把對(duì)某個(gè)類的描述寫在不同地方,甚至寫到兩個(gè)或多個(gè)不同的文件中去。partial 信息只對(duì)編譯器有用,編譯器在編譯時(shí)看到對(duì)某個(gè)類的描述是“碎”的(partial 的),它會(huì)去其他地方收集該類的其他碎片,然后把所有的該類的碎片組合成完整的一個(gè)類,再對(duì)其編譯。所以partial 體現(xiàn)不到編譯好的 il中去的。至于partial類型給我們帶來怎么樣的意義呢?我們以后再討論。
#結(jié)束
qq:14754875
email:[email protected]
bbs:www.shixm.com/bbs