资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何理解ASP.NET2.0泛型

如何理解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泛型方法

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、ASP.NET 2.0泛型抽象类

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、ASP.NET 2.0泛型接口

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、ASP.NET 2.0泛型委托

using System;  using System.Collections.Generic;    public class GenericDelegate  {  // 声明一个泛型委托  public delegate string OutputDelegate  〈T 〉(T t);    // 定义一个静态方法  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));  }  }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


名称栏目:如何理解ASP.NET2.0泛型
网站URL:http://cdkjz.cn/article/joidss.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220