C# 泛型自定义集合
来源:互联网 发布:淘宝折扣app 编辑:程序博客网 时间:2024/06/08 16:56
一.原型
public class InList { int[] arr = new int[10]; int index = 0; public void Add(int param) { if (index >= arr.Length) { int[] tmp = new int[arr.Length * 2]; arr.CopyTo(tmp, 0); arr = tmp; } arr[index] = param; index++; } public int this[int index] { get { if (index >= arr.Length) { throw new Exception("索引越界"); } return arr[index]; } } }
二.泛型自定义集合
/// <summary> /// 自定义泛型类集合 /// </summary> /// <typeparam name="T"></typeparam> public class MyList<T> { T[] arr = new T[10]; int index = 0; public void Add(T param) { if (index >= arr.Length) { T[] tmp = new T[arr.Length * 2]; arr.CopyTo(tmp, 0); arr = tmp; } arr[index] = param; index++; } public T this[int index] { get { if (index >= arr.Length) { throw new Exception("索引越界"); } return arr[index]; } } }
调用
class Program { static void Main(string[] args) { //InList ilist = new InList(); //ilist.Add(1); //ilist.Add(3); //ilist.Add(4); //ilist.Add(7); //Console.WriteLine(ilist[3]); //Console.ReadKey(); MyList<int> ilist = new MyList<int>(); ilist.Add(1); MyList<string> sList = new MyList<string>(); sList.Add("1"); //MyList1.MyList`1[System.String] Console.WriteLine(sList.ToString()); Console.ReadKey(); } }
阅读全文
0 0
- C# 泛型自定义集合
- C#集合 泛型集合
- c# 自定义集合类
- c#自定义集合类
- C#自定义集合
- C#泛型集合
- C#泛型集合
- C#泛型集合
- c#泛型集合
- C#自定义集合初始化器
- C#集合类:Dictionary泛型集合
- C# 集合类:Dictionary 泛型集合
- C#自定义泛型
- C# 自定义泛型
- C# 创建自定义集合编辑器/自定义集合属性
- C#泛型集合揽胜
- C#泛型集合揽胜
- C#泛型集合揽胜
- 来认识世界上第一支AI基金:模仿股票分析师 365天无间断工作
- Linux之vi编辑器学习笔记
- Hadoop实战(5)_Hadoop的运维经验
- python学习记录(四)
- Docker编排工具调研:Rancher
- C# 泛型自定义集合
- jdbc基本学习
- spring boot 2节笔记
- Uber正式解雇其自动驾驶部门负责人Anthony Levandowski
- Oracle-本地连接没问题,远程连接有问题解决方案
- 迎合宅男,日本版Echo引入二次元少女
- 品牌活动新体验 浦桑尼克热气球空投扫地机器人
- springmvc相关面试题
- Core i9 系列登场:农企翻身90度,英特尔也不能“挤牙膏”了 | Computex 2017