[×××]


C# 2.0 泛型


作者:webabcd


介绍
泛型是 C# 2.0 的最强大的功能。通过泛型可以定义类型安全的数据结构,而无须使用实际的数据类型。这能够显著提高性能并得到更高质量的代码,因为您可以重用数据处理算法。


泛型类
/*---
* 如何 使用 和 继承 泛型类
---*/

using System;
using System.Collections.Generic;

// 泛型类
public class GenericClass<T>
{
        // 返回泛型的具体类型和ToString()后的值
        public virtual string Output(T t)
        {
                return "类型:" + t.GetType().ToString() + ";值:" + t.ToString();
        }
}

// 继承自一个泛型类,指定基类的泛型的具体类型
public class InheritClass1 : GenericClass<string>
{

}

// 继承自一个泛型类,基类和子类都是同一泛型
public class InheritClass2<T> : GenericClass<T>
{
        public override string Output(T t)
        {
                return "子类实现-类型:" + t.GetType().ToString() + ";值:" + t.ToString();
        }
}

// 继承自一个泛型类,指定基类的泛型的具体类型,子类仍然可以是泛型
public class InheritClass3<Z> : GenericClass<double>
{
        public string ChildOutput(Z z)
        {
                return "子类实现-类型:" + z.GetType().ToString() + ";值:" + z.ToString();
        }
}


public partial class Generic_Class : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
                GenericClass<DateTime> c = new GenericClass<DateTime>();
                Response.Write(c.Output(DateTime.Now) + "<br />");

                InheritClass1 c1 = new InheritClass1();
                Response.Write(c1.Output("abc") + "<br />");

                GenericClass<Guid> c2 = new InheritClass2<Guid>();
                Response.Write(c2.Output(new Guid()) + "<br />");

                InheritClass3<int> c3 = new InheritClass3<int>();
                Response.Write(c3.Output(Math.PI) + " ||| " + c3.ChildOutput(123) + "<br />");
        }
}
 
运行结果
类型:System.DateTime;值:2007-2-10 22:56:09
类型:System.String;值:abc
子类实现-类型:System.Guid;值:00000000-0000-0000-0000-000000000000
类型:System.Double;值:3.14159265358979 ||| 子类实现-类型:System.Int32;值:123


泛型方法
/*---
* 如何 使用 泛型方法    
---*/

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 />");
        }
}
 
运行结果
类型:System.Int32;值:23
类型:System.DateTime;值:2007-2-10 22:57:29


泛型委托
/*---
* 如何 使用 泛型委托
---*/

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));
        }
}
 
运行结果
Hello, aabbcc
Time, 2007-2-10 22:59:26


抽象泛型类,派生约束
/*---
* 如何 使用 和 继承 抽象泛型类    
* 派生约束
---*/

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要继承自泛型Y
        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 />");
        }
}
 
运行结果
aaa
System.Data.DataTable


泛型接口,派生约束,构造函数约束
/*---
* 如何 使用 泛型接口
* 派生约束
* 构造函数约束(如果实例化的话)
---*/

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 />");
        }
}
 
运行结果
System.Data.DataTable


其它
/*---
* 泛型 其它
---*/

using System;
using System.Collections.Generic;

// 泛型也可以使用别名
using MyList = System.Collections.Generic.List<string>;

public partial class Generic_Other : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
                MyList ml = new MyList();
                ml.Add("aaa");
                ml.Add("bbb");

                Response.Write(ml[0]);
                Response.Write("<br />");
                Response.Write(ml[1]);

                // 其它说明
                // 值类型约束 public class MyClass<T> where T : struct { }
                // 引用类型约束 public class MyClass<T> where T : class { }
                // 没有泛型属性
        }
}
 
 
运行结果
aaa
bbb


OK
[×××]