尊重作者,請保留 www.it55.com 連結字樣。
C#2.0作為#1.X的升級版本,為我們引入了很多新的而且很實用的特性。最重要的當屬泛型(Generics)、匿名方法(Anonymous Methods)、迭代器(Iterators)和局部類(partial Types)。這些新特性在提供高度相容性的同時,也在很大程度上提高了代碼的效率和安全性。
本節我們學習有關於泛型的內容。
泛型存在的必要性:在1.X版本中,為了能適應不同類型的參數引入,我們常常需要重寫一些函數,或者常常將其object化,以達到函數的通用性。但往往帶給我們的是程式效能的下降和重複性勞動的增加。泛型的出現很好的解決了這個問題。其實簡單的講,泛型是一種可以傳遞或者靈活規範參數類型的機制。
泛型需要命名空間System.Collections.Generic的支援,可應用於類、方法、結構、介面、委託等設計中,集複用性、型別安全、高效率於一身。下面我們分別舉例來看看泛型的幾種使用方法。
1、泛型方法
using System;
using System.Collections.Generic;
public class GenericMethod
{
// 靜態 泛型方法
public static string Output<T>(T t)
{
return "類型:" + t.GetType().ToString() + ";值:" + t.ToString();
}
}
public partial class Generic_Method : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(GenericMethod.Output<int>(23) + "<br />");
Response.Write(GenericMethod.Output<DateTime>(DateTime.Now) + "<br />");
}
}
2、泛型抽象類別
using System;
using System.Collections.Generic;
// 泛型抽象類別
public abstract class GenericParent
{
// 泛型抽象方法,傳回值為一個泛型,加一個約束使泛型X要繼承自泛型Y
public abstract X Output<X, Y>(X x, Y y) where X : Y;
// 泛型抽象方法,傳回值為一個string類型,加一個約束使泛型X要繼承自IListSource
public abstract string Output2<X>(X x) where X : System.ComponentModel.IListSource;
}
public class GenericChild : GenericParent
{
// 重寫抽象類別的泛型方法
public override T Output<T, Z>(T t, Z z)
{
return t;
}
// 重寫抽象類別的泛型方法
public override string Output2<T>(T t)
{
return t.GetType().ToString();
}
}
public partial class Generic_Abstract : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GenericChild gc = new GenericChild();
Response.Write(gc.Output<string, IComparable>("aaa", "xxx"));
Response.Write("<br />");
Response.Write(gc.Output2<System.Data.DataTable>(new System.Data.DataTable()));
Response.Write("<br />");
}
}
3、泛型介面
using System;
using System.Collections.Generic;
// 泛型介面
public interface IGenericInterface<T>
{
T CreateInstance();
}
// 實現上面泛型介面的泛型類
// 派生約束where T : TI(T要繼承自TI)
// 建構函式約束where T : new()(T可以執行個體化)
public class Factory<T, TI> : IGenericInterface<TI>
where T : TI, new()
{
public TI CreateInstance()
{
return new T();
}
}
public partial class Generic_Interface : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IGenericInterface<System.ComponentModel.IListSource> factory =
new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();
Response.Write(factory.CreateInstance().GetType().ToString());
Response.Write("<br />");
}
}
4、泛型委派
using System;
using System.Collections.Generic;
public class GenericDelegate
{
// 聲明一個泛型委派
public delegate string OutputDelegate<T>(T t);#p#分網頁標題#e#
// 定義一個靜態方法
public static string DelegateFun(string s)
{
return String.Format("Hello, {0}", s);
}
// 定義一個靜態方法
public static string DelegateFun(DateTime dt)
{
return String.Format("Time, {0}", dt.ToString());
}
}
public partial class Generic_Delegate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 使用泛型委派
GenericDelegate.OutputDelegate<string> delegate1
= new GenericDelegate.OutputDelegate<string>(GenericDelegate.DelegateFun);
Response.Write(delegate1("aabbcc"));
Response.Write("<br />");
// 使用泛型委派(匿名方法)
GenericDelegate.OutputDelegate<DateTime> delegate2 = GenericDelegate.DelegateFun;
Response.Write(delegate2(DateTime.Now));
}
}
下一節我們將繼續學習C# 2.0的新特性 匿名方法、迭代器、局部類