当前位置:首页>网络学院>程序开发>ASP.NET教程>文章内容

asp.net 2.0教程 C# 2.0新特性 泛型

[ 来源:http://www.it55.com | 作者: | 时间:2007-10-08 | 收藏 | 推荐 ] 【

尊重作者,请保留 www.it55.com 链接字样。

http://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; 免费设计素材下载http://www.it55.com

public class GenericMethod
{
    // 静态 泛型方法
    public static string Output<T>(T t)
    {
        return "类型:" + t.GetType().ToString() + ";值:" + t.ToString();
    }
}

http://www.it55.com

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 />");
    }
} http://www.it55.com/

2、泛型抽象类
using System;
using System.Collections.Generic; 免费资源http://www.it55.com

// 泛型抽象类
public abstract class GenericParent
{
    // 泛型抽象方法,返回值为一个泛型,加一个约束使泛型X要继承自泛型Y
    public abstract X Output<X, Y>(X x, Y y) where X : Y; 免费设计素材下载http://www.it55.com

    // 泛型抽象方法,返回值为一个string类型,加一个约束使泛型X要继承自IListSource
    public abstract string Output2<X>(X x) where X : System.ComponentModel.IListSource;
}

精美商业网页模版下载http://www.it55.com

public class GenericChild : GenericParent
{
    // 重写抽象类的泛型方法
    public override T Output<T, Z>(T t, Z z)
    {
        return t;
    } http://www.it55.com在线教程

    // 重写抽象类的泛型方法
    public override string Output2<T>(T t)
    {
        return t.GetType().ToString();
    }
}

免费壁纸下载http://www.it55.com


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 />");

免费网页模版下载http://www.it55.com

        Response.Write(gc.Output2<System.Data.DataTable>(new System.Data.DataTable()));
        Response.Write("<br />");
    }
}

sflj http://www.it55.com kg^&fgd

3、泛型接口
using System;
using System.Collections.Generic; 免费矢量图片素材下载http://www.it55.com

// 泛型接口
public interface IGenericInterface<T>
{
    T CreateInstance();
} IT资讯之家 http://www.it55.com

// 实现上面泛型接口的泛型类
// 派生约束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();
    }
} it55.com


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>(); 免费资源http://www.it55.com

        Response.Write(factory.CreateInstance().GetType().ToString());
        Response.Write("<br />");
    }
}

精美韩国模版下载http://www.it55.com

4、泛型委托
using System;
using System.Collections.Generic;

http://www.it55.com在线教程

public class GenericDelegate
{
    // 声明一个泛型委托
    public delegate string OutputDelegate<T>(T t);

(编辑:IT资讯之家 www.it55.com

返回顶部
共2页: 上一页 1 [2] 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]