温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何理解ASP.NET 2.0泛型

发布时间:2021-11-26 17:42:22 来源:亿速云 阅读:119 作者:柒染 栏目:编程语言

如何理解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));  }  }

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI