1. 程式人生 > >C# 2.0 ,.NET Framework 2.0,Visual Studio 2005

C# 2.0 ,.NET Framework 2.0,Visual Studio 2005

ons var 設計器 als can valid del 窗體 PE

C#2.0對應.net Frmework2.0,開始版本工具是Visual Studio2005

C#2.0主要添加了泛型、匿名方法,分部類型(類、結構、接口),可空類型,叠代器,屬性訪問控制器,方法組轉換,協變和逆變,靜態類、委托推斷

1、Generics:泛型

 1         public static bool GreatTo<T>(T t,T t1) where T : IComparable
 2         {
 3             if (t.CompareTo(t1) > 0)
 4             {
 5                 return
true; 6 } 7 else 8 { 9 return false; 10 } 11 }

2、Anonymous methods:匿名方法

this.btnValidateCode.BeginInvoke(new MethodInvoker(() => { this.btnValidateCode.Text = "Cancel"; }));

3、Partial types:分部類型,可以將類、結構、接口等類型定義拆分到多個文件中,使用關鍵字partial。最常見的就是WinForm中窗體的業務部分和設計器部分

技術分享圖片
 1  public partial class Form1 : Form
 2     {
 3 
 4     }
 5 
 6 
 7  partial class Form1
 8     {
 9         /// <summary>
10         /// 必需的設計器變量。
11         /// </summary>
12         private System.ComponentModel.IContainer components = null;
13 
14         /// <summary>
15         /// 清理所有正在使用的資源。
16 /// </summary> 17 /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> 18 protected override void Dispose(bool disposing) 19 { 20 if (disposing && (components != null)) 21 { 22 components.Dispose(); 23 } 24 base.Dispose(disposing); 25 } 26 27 #region Windows 窗體設計器生成的代碼 28 29 /// <summary> 30 /// 設計器支持所需的方法 - 不要 31 /// 使用代碼編輯器修改此方法的內容。 32 /// </summary> 33 private void InitializeComponent() 34 { 35 this.SuspendLayout(); 36 // 37 // Form1 38 // 39 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 40 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 41 this.ClientSize = new System.Drawing.Size(284, 261); 42 this.Name = "Form1"; 43 this.Text = "Form1"; 44 this.ResumeLayout(false); 45 46 } 47 48 #endregion 49 }
View Code

4、Nullable types:可以為Null的類型,該類可以是其它值或者null

1         public int? Age;
2         public decimal? Price;
3         public bool? Flag;

5、Iterators:叠代器


6、Getter/setter separate accessibility:屬性訪問控制

public string UserName { get; set; }


7、Method group conversions (delegates):方法組轉換,可以將聲明委托代表一組方法,隱式調用

 1   public delegate void GreetHandler(string name);
 2 
 3         public void Test()
 4         {
 5             GreetHandler greetHander = ChineseGreet;
 6            //GreetHandler greetHander = new GreetHandler( ChineseGreet);  //可以不用寫成這樣子
 7             greetHander("jim");
 8         }
 9         public void ChineseGreet(string name)
10         {
11             Console.WriteLine("您好" + name);
12         }

8、Co- and Contra-variance for delegates and interfaces:委托、接口的協變和逆變


9、Static classes:靜態類

C# 2.0 ,.NET Framework 2.0,Visual Studio 2005