C# 泛型自定义集合
2017-10-29 12:22
246 查看
一.原型
二.泛型自定义集合
调用
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(); } }
相关文章推荐
- [c#基础]泛型集合的自定义类型排序
- C#集合,泛型,以及自定义排序自己总结1
- C# 基于泛型的自定义线性节点链表集合示例
- [我眼中的C#]集合和泛型
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
- C#中关于在List集合类中使用泛型的例子
- C#中的接口和泛型集合探讨
- C#创建自定义泛型举例
- 编写高质量代码改善C#程序的157个建议[泛型集合、选择集合、集合的安全]
- C# Synchronized 和 SyncRoot 实现线程同步的源码分析及泛型集合的线程安全访问
- C#基础(18)——List泛型集合、装箱、拆箱、字典集合与Hashtable区别
- .NET/C#中对自定义对象集合进行自定义排序的方法
- C#基础知识(十一)——泛型集合、文件管理、多态、接口
- C#中存储数据的集合:数组、集合、泛型、字典
- C#读取数据库返回泛型集合(DataSetToList)
- C#读取数据库返回泛型集合(DataSetToList)
- C# 泛型集合实例应用浅析
- C#集合与泛型
- C#各种泛型集合体验
- C# 泛型集合List和非泛型集合ArrayList的性能比较