2011-11-8

今天讲了泛类型
就是这个类型代表任何的类型,呵呵,挺奇妙
下面附上我上课时跟着写的代码吧
这是一个类:
public class Point<T>               //泛类型
{
    private T xPos;
    private T yPos;
    public T X
    {
        get { return this.xPos; }
        set { this.xPos = value; }
    }
    public T Y
    {
        get { return this.yPos; }
        set { this.yPos = value; }
    }
    public override string ToString()
    {
        return string.Format("X:{0}Y:{1}",xPos,yPos);
    }
public Point(T x,T y)
{
        xPos = x;
        yPos = y;
}
}

这是一个方法

protected void Page_Load(object sender, EventArgs e)
    {


        int a = 10;
        int b = 20;
        //Swap(ref a,ref b);
        this.Swap(ref a, ref b);
        Response.Write(string.Format("a:{0};b:{1}", a, b));
    }
    public void Swap(ref int a, ref int b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
    public void Swap(ref float a, ref float b)
    {
        float temp;
        temp = a;
        a = b;
        b = temp;
    }
    public void Swap<T>(ref T a, ref T b)         //泛型方法
    {
        T temp;
        temp = a;
        a = b;
        b = temp;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值