zoukankan      html  css  js  c++  java
  • asp.net 2.0教程 C# 2.0新特性 泛型

    尊重作者,请保留 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;

    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、泛型抽象类
    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、泛型接口
    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、泛型委托
    using System;
    using System.Collections.Generic;

    public class GenericDelegate
    {
        // 声明一个泛型委托
        public delegate string OutputDelegate<T>(T t);#p#分页标题#e#

        // 定义一个静态方法
        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));
        }
    }

    下一节我们将继续学习C# 2.0的新特性 匿名方法、迭代器、局部类

  • 相关阅读:
    第12组 Alpha冲刺(4/6)
    第12组 Alpha冲刺(3/6)
    第12组 Alpha冲刺(2/6)
    2019软件工程实践——第四次作业
    2019软件工程实践——第三次作业
    2019软件工程实践——第二次作业
    2019软件工程实践——第一次作业
    博客园 自定义背景图片(包括动图)
    软件工程 实验一 GIT代码版本管理
    WordCounter项目(基于javase)
  • 原文地址:https://www.cnblogs.com/bk/p/917038.html
Copyright © 2011-2022 走看看