泛型的几种用法

1.泛型类

    /** <summary>     /// 定义一个泛型类,该类有两个类型参数,分别是T,S     /// http://pw.cnblogs.com     /// </summary>     /// <typeparam name="T">类型参数</typeparam>     /// <typeparam name="S">类型参数</typeparam>     public class Test<T,S>     {         //泛型类的类型参数可用于类成员         private T name;         private S age;

        public Test(T Name,S Age)         {             this.name = Name;             this.age = Age;         }

        public void SetValue()         {             Console.WriteLine(name.ToString());             Console.WriteLine(age.ToString());         }     }

 

2.泛型类得继承,继承规则如下:

(1)、泛型类继承中,父类的类型参数已被实例化,这种情况下子类不一定必须是泛型类;
(2)、父类的类型参数没有被实例化,但来源于子类,也就是说父类和子类都是泛型类,并且二者有相同的类型参数;

    public class TestChild : Test<string, int>{ }

    public class TestChild<T, S> : Test<T, S> { }

    public class TestChild<T, S> : Test<String, int> { }


3.泛型接口,其创建以及继承规则和上面说的泛型类是一样的,看下面的代码:

public interface IList<T> 
    {
        T[] GetElements();
    } 
    public interface IDictionary<K,V>             
    {
        void Add(K key, V value); 
    } 

    // 泛型接口的类型参数要么已实例化
    // 要么来源于实现类声明的类型参数
    class List<T> : IList<T>, IDictionary<int, T> 
    {
        public T[] GetElements() { return null; }
        public void Add(int index, T value) 
        { 
        }
    }


4.泛型委托

using System;
using System.Collections.Generic;
using System.Text;

namespace GenericTest
{
    //定义一个委托,类型参数为T,返回值类型T
    //泛型委托支持在返回值和参数上应用类型参数
    delegate string GenericDelete<T>(T value);

    class test
    {
        static string F(int i) { return "SHY520"; }
        static string G(string s) { return "SHY520"; }

        static void Main(string[] args)
        {
            GenericDelete<string> G1 = G;
            GenericDelete<int> G2 = new GenericDelete<int>(F);
        }
    } 
}


5.泛型方法

using System;
using System.Collections.Generic;
using System.Text;

namespace GenericTest
{
    class GenericClass
    {
        //申明一个泛型方法
        public T getvalue<T>(T t)
        {
            return t;
        }

        //调用泛型方法
        //注意:在调用泛型方法时,对泛型方法的类型参数实例化
        public int useMethod()
        {
            return this.getvalue<int>(10);
        }

        //重载getvalue方法
        public int getvalue(int i)
        {
            return i;
        }
    }
    //下面演示覆盖
    //要注意的是,泛型方法被覆盖时,约束被默认继承,不需要重新指定约束关系
    abstract class Parent
    {
        public abstract K TEST<K, V>(K k, V v) where K : V;

    }

    class Child : Parent
    {
        public override T TEST<T, S>(T t, S s)
        {
            return t;
        }
    }
}

6.泛型中的约束:

C#中的泛型只支持显示的约束,因为这样才能保证C#所要求的类型安全,但显示的约束并非时必须的,如果不加约束,泛型类型参数将只能访问System.Object类型中的公有方法。“显式约束”由where子句表达,可以指定“基类约束”,“接口约束”,“构造器约束”,“值类型/引用类型约束”共四种约束。

1、基类约束:

class A { 
    public void F1() {} 
} 
    class B { public void F2() {} } 
    class C<S,T> 
    where S: A // S继承自A 
    where T: B // T继承自B 
    { 
    // 可以在类型为S的变量上调用F1,
    // 可以在类型为T的变量上调用F2 
    } 


 

2、接口约束:

interface IPrintable { void Print(); 
    }
    interface IComparable<T> { int CompareTo(T v);}
    interface IKeyProvider<T> { T GetKey(); }
    class Dictionary<K,V> 
    where K: IComparable<K> 
    where V: IPrintable, IKeyProvider<K> 
    { 
    // 可以在类型为K的变量上调用CompareTo, 
    // 可以在类型为V的变量上调用Print和GetKey 
    }

 

3、构造器约束:

class A { public A() { } } 
class B { public B(int i) { } } 
class C<T> 
where T : new() 
{ 
//可以在其中使用T t=new T();  
} 
C<A> c=new C<A>(); //可以,A有无参构造器
C<B> c=new C<B>(); //错误,B没有无参构造器

4、值/引用类型约束

public struct A {  } 
public class B {  } 
class C<T> 
where T : struct 
{ 
// T在这里面是一个值类型 
} 
C<A> c=new C<A>(); //可以,A是一个值类型
C<B> c=new C<B>(); //错误,B是一个引用类型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值