最新军情观察室,美国政治避难,谏山实生
1 using system; 2 using system.collections.generic; 3 4 namespace generictest 5 { 6 public class testgeneric<t> 7 { 8 9 private t[] array; 10 public testgeneric(int i) 11 { 12 array = new t[i + 1]; 13 } 14 public t getitem(int index) 15 { 16 return array[index]; 17 } 18 public void setitem(int index, t value) 19 { 20 array[index] = value; 21 } 22 } 23 24 class tester 25 { 26 static void main(string[] args) 27 { 28 testgeneric<char> myarray = new testgeneric<char>(5); 29 for (int i = 0; i < 5; i++) 30 { 31 myarray.setitem(i, (char)(i + 97)); 32 } 33 34 for (int i=0; i<5; i++) 35 { 36 console.writeline(myarray.getitem(i)); 37 } 38 console.writeline(); 39 console.readkey(); 40 } 41 42 } 43 }
结果
约束
约束限定条件
例:
public class myarray<t> : b<t> where t : new() { }
定义多个类型参数和约束:
public class base<a,b,c> where a: struct where b: new() where c: class { }
泛型也可以继承泛型:
class d:c<string,int> class e<u,v>:c<u,v> class f<u,v>:c<string,int>
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论