ASP.NET 2.0泛型詳解
C#2.0作為#1.X的升級(jí)版本,為我們引入了很多新的而且很實(shí)用的特性。最重要的當(dāng)屬ASP.NET 2.0泛型(Generics)、匿名方法(Anonymous Methods)、迭代器(Iterators)和局部類(lèi)(partial Types)。這些新特性在提供高度兼容性的同時(shí),也在很大程度上提高了代碼的效率和安全性。
本節(jié)我們學(xué)習(xí)有關(guān)于ASP.NET 2.0泛型的內(nèi)容。泛型存在的必要性:在1.X版本中,為了能適應(yīng)不同類(lèi)型的參數(shù)引入,我們常常需要重寫(xiě)一些函數(shù),或者常常將其object化,以達(dá)到函數(shù)的通用性。但往往帶給我們的是程序性能的下降和重復(fù)性勞動(dòng)的增加。泛型的出現(xiàn)很好的解決了這個(gè)問(wèn)題。其實(shí)簡(jiǎn)單的講,泛型是一種可以傳遞或者靈活規(guī)范參數(shù)類(lèi)型的機(jī)制。
泛型需要命名空間System.Collections.Generic的支持,可應(yīng)用于類(lèi)、方法、結(jié)構(gòu)、接口、委托等設(shè)計(jì)中,集復(fù)用性、類(lèi)型安全、高效率于一身。下面我們分別舉例來(lái)看看泛型的幾種使用方法。
1、ASP.NET 2.0泛型方法
- using System;
- using System.Collections.Generic;
- public class GenericMethod
- {
- // 靜態(tài) 泛型方法
- public static string Output〈T 〉(T t)
- {
- return "類(lèi)型:" + 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、ASP.NET 2.0泛型抽象類(lèi)
- using System;
- using System.Collections.Generic;
- // 泛型抽象類(lèi)
- public abstract class GenericParent
- {
- // 泛型抽象方法,返回值為一個(gè)泛型,
- 加一個(gè)約束使泛型X要繼承自泛型Y
- public abstract X Output〈 X, Y 〉
- (X x, Y y) where X : Y;
- // 泛型抽象方法,返回值為一個(gè)string類(lèi)型,
- 加一個(gè)約束使泛型X要繼承自IListSource
- public abstract string Output2〈 X 〉
- (X x) where X : System.ComponentModel.
- IListSource;
- }
- public class GenericChild : GenericParent
- {
- // 重寫(xiě)抽象類(lèi)的泛型方法
- public override T Output〈 T, Z 〉 (T t, Z z)
- {
- return t;
- }
- // 重寫(xiě)抽象類(lèi)的泛型方法
- 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 / 〉 ");
- }
- }
#p#
3、ASP.NET 2.0泛型接口
- using System;
- using System.Collections.Generic;
- // 泛型接口
- public interface IGenericInterface〈T 〉
- {
- T CreateInstance();
- }
- // 實(shí)現(xiàn)上面泛型接口的泛型類(lèi)
- // 派生約束where T : TI(T要繼承自TI)
- // 構(gòu)造函數(shù)約束where T : new()(T可以實(shí)例化)
- 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、ASP.NET 2.0泛型委托
- using System;
- using System.Collections.Generic;
- public class GenericDelegate
- {
- // 聲明一個(gè)泛型委托
- public delegate string OutputDelegate
- 〈T 〉(T t);
- // 定義一個(gè)靜態(tài)方法
- public static string DelegateFun
- (string s)
- {
- return String.Format("Hello, {0}", s);
- }
- // 定義一個(gè)靜態(tài)方法
- 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));
- }
- }
【編輯推薦】
- ASP.NET電子商務(wù)系統(tǒng)設(shè)計(jì)淺析(1)
- 基于ASP.NET圖書(shū)電子商務(wù)網(wǎng)站建設(shè)技術(shù)探析
- ASP.NET用Post方式向網(wǎng)頁(yè)發(fā)送數(shù)據(jù)
- ASP.NET 2.0部署WEB應(yīng)用程序淺析
- ASP.NET中的HttpWorkerRequest對(duì)像
- 介紹ASP.NET MVC框架