• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Dreama
只想要简简单单的快乐!
博客园    首页    新随笔    联系   管理     
C#泛型类的简单创建与使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            Test<string,int> t = new Test<string,int>("Age",16);
            t.Run();
            t.SetT1Value = "Height";
            t.SetT2Value = 1000;
            t.Run();
            Console.ReadKey();
        }
    }

    class Test<T1,T2>
    {
        private T1 _t1;
        private T2 _t2;

        public Test(T1 v1,T2 v2)
        {
            _t1 = v1;
            _t2 = v2;
        }

        public T1 SetT1Value
        {
            set
            {
                _t1 = value;
            }
        }

        public T2 SetT2Value
        {
            set
            {
                _t2 = value;
            }
        }

        public void Run()
        {
            Console.WriteLine("The type of T1 is {0},the value is {1}",_t1.GetType().ToString(),_t1);
            Console.WriteLine("The type of T2 is{0},the value is {1}", _t2.GetType().ToString(), _t2);
        }

    }
}

运行结果:

posted on 2013-11-20 22:52  Dreamma  阅读(502)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3

© 本文章版权归 Dreama 所有, 转载授权请联系: cnxy@88.com

如果本文对您有帮助,欢迎支持原创

支付宝

支付宝扫码支持

微信

微信赞赏支持