ASP.NET 2.0泛型详解

开发 后端
本文向您介绍ASP.NET 2.0泛型特性。在1.X版本中,为了能适应不同类型的参数引入,我们常常需要重写一些函数,或者常常将其object化,以达到函数的通用性。ASP.NET 2.0泛型的出现很好的解决了这个问题。 设计中,集复用性、类型安全、高效率于一身

C#2.0作为#1.X的升级版本,为我们引入了很多新的而且很实用的特性。最重要的当属ASP.NET 2.0泛型(Generics)、匿名方法(Anonymous Methods)、迭代器(Iterators)和局部类(partial Types)。这些新特性在提供高度兼容性的同时,也在很大程度上提高了代码的效率和安全性。

本节我们学习有关于ASP.NET 2.0泛型的内容。泛型存在的必要性:在1.X版本中,为了能适应不同类型的参数引入,我们常常需要重写一些函数,或者常常将其object化,以达到函数的通用性。但往往带给我们的是程序性能的下降和重复性劳动的增加。泛型的出现很好的解决了这个问题。其实简单的讲,泛型是一种可以传递或者灵活规范参数类型的机制。
泛型需要命名空间System.Collections.Generic的支持,可应用于类、方法、结构、接口、委托等设计中,集复用性、类型安全、高效率于一身。下面我们分别举例来看看泛型的几种使用方法。

1、ASP.NET 2.0泛型方法

  1. using System;  
  2. using System.Collections.Generic;   
  3.  
  4. public class GenericMethod  
  5. {  
  6. // 静态 泛型方法  
  7. public static string Output〈T 〉(T t)  
  8. {  
  9. return "类型:" + t.GetType().  
  10. ToString() + ";值:" + t.ToString();  
  11. }  
  12. }   
  13.  
  14. public partial class Generic_Method :   
  15. System.Web.UI.Page  
  16. {  
  17. protected void Page_Load(object   
  18. sender, EventArgs e)  
  19. {  
  20. Response.Write(GenericMethod.Output 
  21. int 〉 (23) + "〈br / 〉 ");  
  22.  
  23. Response.Write(GenericMethod.Output 
  24. 〈DateTime 〉 (DateTime.Now) + "〈br / 〉 ");  
  25. }  
  26. }   

2、ASP.NET 2.0泛型抽象类

  1. using System;  
  2. using System.Collections.Generic;  
  3.  
  4. // 泛型抽象类  
  5. public abstract class GenericParent  
  6. {  
  7. // 泛型抽象方法,返回值为一个泛型,  
  8. 加一个约束使泛型X要继承自泛型Y  
  9. public abstract X Output〈 X, Y 〉   
  10. (X x, Y y) where X : Y;   
  11.  
  12. // 泛型抽象方法,返回值为一个string类型,  
  13. 加一个约束使泛型X要继承自IListSource  
  14. public abstract string Output2〈 X 〉   
  15. (X x) where X : System.ComponentModel.  
  16. IListSource;  
  17. }   
  18.  
  19. public class GenericChild : GenericParent  
  20. {  
  21. // 重写抽象类的泛型方法  
  22. public override T Output〈 T, Z 〉 (T t, Z z)  
  23. {  
  24. return t;  
  25. }    
  26.  
  27. // 重写抽象类的泛型方法  
  28. public override string Output2〈 T 〉 (T t)  
  29. {  
  30. return t.GetType().ToString();  
  31. }  
  32. }   
  33.  
  34. public partial class Generic_Abstract :  
  35.  System.Web.UI.Page  
  36. {  
  37. protected void Page_Load(object sender,   
  38. EventArgs e)  
  39. {  
  40. GenericChild gc = new GenericChild();  
  41. Response.Write(gc.Output〈 string, IComparable 〉   
  42. ("aaa""xxx"));  
  43. Response.Write("〈 br / 〉 ");   
  44.  
  45. Response.Write(gc.Output2〈 System.Data.DataTable 〉   
  46. (new System.Data.DataTable()));  
  47. Response.Write("〈 br / 〉 ");  
  48. }  
  49. }   

#p#

3、ASP.NET 2.0泛型接口

  1. using System;  
  2. using System.Collections.Generic;   
  3.  
  4. // 泛型接口  
  5. public interface IGenericInterface〈T 〉  
  6. {  
  7. T CreateInstance();  
  8. }   
  9.  
  10. // 实现上面泛型接口的泛型类  
  11. // 派生约束where T : TI(T要继承自TI)  
  12. // 构造函数约束where T : new()(T可以实例化)  
  13. public class Factory〈T, TI 〉 :   
  14. IGenericInterface〈TI 〉  
  15. where T : TI, new()  
  16. {  
  17. public TI CreateInstance()  
  18. {  
  19. return new T();  
  20. }  
  21. }   
  22.  
  23. public partial class Generic_Interface :   
  24. System.Web.UI.Page  
  25. {  
  26. protected void Page_Load(object sender,   
  27. EventArgs e)  
  28. {  
  29. IGenericInterface〈System.ComponentModel.  
  30. IListSource 〉factory =  
  31. new Factory〈System.Data.DataTable,   
  32. System.ComponentModel.IListSource 〉();   
  33.  
  34. Response.Write(factory.CreateInstance().  
  35. GetType().ToString());  
  36. Response.Write("〈br / 〉");  
  37. }  
  38. }   

4、ASP.NET 2.0泛型委托

  1. using System;  
  2. using System.Collections.Generic;   
  3.  
  4. public class GenericDelegate  
  5. {  
  6. // 声明一个泛型委托  
  7. public delegate string OutputDelegate  
  8. 〈T 〉(T t);   
  9.  
  10. // 定义一个静态方法  
  11. public static string DelegateFun  
  12. (string s)  
  13. {  
  14. return String.Format("Hello, {0}", s);  
  15. }   
  16.  
  17. // 定义一个静态方法  
  18. public static string DelegateFun  
  19. (DateTime dt)  
  20. {  
  21. return String.Format("Time, {0}",   
  22. dt.ToString());  
  23. }  
  24. }   
  25.  
  26.  
  27. public partial class Generic_Delegate :   
  28. System.Web.UI.Page  
  29. {  
  30. protected void Page_Load(object sender,  
  31. EventArgs e)  
  32. {  
  33. // 使用泛型委托  
  34. GenericDelegate.OutputDelegate〈string 〉   
  35. delegate1  
  36. = new GenericDelegate.OutputDelegate  
  37. 〈string 〉(GenericDelegate.DelegateFun);   
  38.  
  39. Response.Write(delegate1("aabbcc"));  
  40. Response.Write("〈br / 〉");   
  41.  
  42. // 使用泛型委托(匿名方法)  
  43. GenericDelegate.OutputDelegate〈DateTime 〉  
  44. delegate2 = GenericDelegate.DelegateFun;  
  45. Response.Write(delegate2(DateTime.Now));  
  46. }  
  47. }   

【编辑推荐】

  1. ASP.NET电子商务系统设计浅析(1)
  2. 基于ASP.NET图书电子商务网站建设技术探析
  3. ASP.NET用Post方式向网页发送数据
  4. ASP.NET 2.0部署WEB应用程序浅析
  5. ASP.NET中的HttpWorkerRequest对像
  6. 介绍ASP.NET MVC框架
    责任编辑:冰荷 来源: it55
    相关推荐

    2009-07-22 17:23:03

    XmlDataSourASP.NET 2.0

    2009-07-30 15:17:16

    ASP.NET 2.0

    2009-07-29 16:08:07

    ASP和ASP.NET

    2009-08-05 11:14:33

    ASP.NET ISA

    2009-07-22 16:25:41

    ASP.NET AJA

    2009-07-24 10:14:22

    ASP.NET开发

    2009-07-28 16:57:50

    ASP.NET Ses

    2009-07-23 13:19:51

    2009-12-14 17:50:45

    ASP.NET 2.0

    2009-07-22 17:21:27

    ASP.NET 2.0

    2009-07-28 14:06:28

    ASP.NET 2.0

    2009-01-07 11:38:22

    ASP.NET.NET错误记录

    2009-07-30 14:55:43

    ASP.NET 2.0

    2009-07-21 14:37:13

    Profile Pro优化ASP.NET 2

    2009-05-11 13:48:00

    ASP.NET 2.0缓存效率

    2009-07-23 13:09:23

    2009-08-04 10:43:59

    ASP.NET控件开发

    2009-07-28 13:39:44

    加载ViewStateASP.NET

    2009-08-19 13:44:00

    ASP.NET Lis

    2009-08-04 18:10:35

    ASP.NET动态编译
    点赞
    收藏

    51CTO技术栈公众号