欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#泛型类创建与使用的方法

程序员文章站 2023-12-02 23:06:52
本文实例为大家分享了c#泛型类创建与使用的具体代码,供大家参考,具体内容如下 using system; using system.collections.ge...

本文实例为大家分享了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);
    }

  }
}

运行结果:

C#泛型类创建与使用的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。