C#泛型類創(chuàng)建與使用的方法
更新時間:2020年06月29日 15:11:19 作者:cnc
這篇文章主要為大家詳細(xì)介紹了C#泛型類創(chuàng)建與使用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#泛型類創(chuàng)建與使用的具體代碼,供大家參考,具體內(nèi)容如下
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); } } }
運行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
【C#基礎(chǔ)】Substring截取字符串的方法小結(jié)(推薦)
這篇文章主要介紹了Substring截取字符串方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05