泛型类和扩展方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 泛型类扩展方法
{
    class Holder<T>
    {
        T[] vars = new T[3];
    public Holder(T v0,T v1,T v2)
    {
        vars[0] = v0;
        vars[1] = v1;
        vars[2] = v2;    
    }
    public T[] GetValues() 
    {
        return vars;
    }
    }
    /*扩展方法的要求:
     *1、声明为static
     *2、是静态类的成员
     *3、第一个参数类型中必须有关键词this,后面是扩展的泛型类的名字
     */
    static class ExtendHolder
    {
        public static void Print<T>(this Holder<T> h)
        {
            T[] vlas = h.GetValues();
            Console.WriteLine("{0},\t{1},\t{2}",vlas[0],vlas[1],vlas[2]);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var intHolder = new Holder<int>(3, 5, 7);
            var stringHolder = new Holder<string>("asd","dfew","vrfa");
            intHolder.Print();
            stringHolder.Print();
            Console.ReadKey();
        }
    }
}

转载于:https://www.cnblogs.com/sulong/p/4801459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值