泛型类和扩展方法
2015-09-11 17:12
204 查看
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(); } }}
相关文章推荐
- ActionBar
- 程序员所应具备的品质
- java判断文字中是否包含URL
- mysql/mariadb性能调优的15个技巧
- 响应式网页设计:rem、em设置网页字体大小自适应
- spring框架包下载地址
- JavaScript HTML DOM - 改变CSS
- NSRunLoop漫谈
- 3.3final中,bit.lshift报错nil value
- stl的list排序
- rebbitmq-RPC(C#)
- Ubuntu 无法开启解决办法
- 数字签名是什么?
- 原始套接字实现Ping命令
- Tomcat Ip 限制
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- 下次要写约瑟夫和并查集
- nginx屏蔽指定URI
- mysql备份之xtrabackup
- 开发手记9.11