自定义类型

  1. public class Product
  2. {
  3. public int Id { get; set; } // 自增ID
  4. public string Name { get; set; } // 名称
  5. public string Code { get; set; } // 主键
  6. public string Category { get; set; } // 类型
  7. public decimal Price { get; set; } // 价格
  8. public DateTime ProduceDate { get; set; } //产品时间
  9.  
  10. /// <summary>
  11. /// 重写ToString 方法
  12. /// </summary>
  13. /// <returns></returns>
  14. public override string ToString()
  15. {
  16. return String.Format("{0}{1}{2}{3}{4}{5}",
  17. this.Id.ToString().PadLeft(2), this.Category.PadLeft(15),
  18. this.Code.PadLeft(7), this.Name.PadLeft(17), this.Price.ToString().PadLeft(8),
  19. this.ProduceDate.ToString("yyyy-M-d").PadLeft(13));
  20. }
  21. public static ProductCollection GetSampleCollection()
  22. {
  23. ProductCollection collection = new ProductCollection(
  24. new Product { Id = 1, Code = "1001", Category = "Red Wine", Name = "Torres Coronas", Price = 285.5m, ProduceDate = DateTime.Parse("1997-12-8") },
  25. new Product { Id = 3, Code = "2001", Category = "White Spirit", Name = "Mao Tai", Price = 1680m, ProduceDate = DateTime.Parse("2001-5-8") },
  26. new Product { Id = 4, Code = "2013", Category = "White Spirit", Name = "Wu Liang Ye", Price = 1260m, ProduceDate = DateTime.Parse("2005-8-1") },
  27. new Product { Id = 8, Code = "3001", Category = "Beer", Name = "TSINGTAO", Price = 6.5m, ProduceDate = DateTime.Parse("2012-4-21") },
  28. new Product { Id = 11, Code = "1003", Category = "Red Wine", Name = "Borie Noaillan", Price = 468m, ProduceDate = DateTime.Parse("1995-7-6") },
  29. new Product { Id = 15, Code = "1007", Category = "Red Wine", Name = "Pinot Noir Rose", Price = 710m, ProduceDate = DateTime.Parse("1988-9-10") },
  30. new Product { Id = 17, Code = "3009", Category = "Beer", Name = "Kingway", Price = 5.5m, ProduceDate = DateTime.Parse("2012-6-13") }
  31. );
  32. return collection;
  33. }
  34. }

  自定义的集合方法

  1. public class ProductCollection
  2. {
  3. public Hashtable table;
  4. public ProductCollection()
  5. {
  6. table = new Hashtable();
  7. }
  8.  
  9. public ProductCollection(params Product[] array)
  10. {//初始化
  11. table = new Hashtable();
  12. foreach (Product item in array)
  13. {
  14. this.Add(item);
  15. }
  16. }
  17.  
  18. public ICollection Keys { get { return table.Keys; } }
  19.  
  20. /// <summary>
  21. /// 根据索引获取当前Key值
  22. /// </summary>
  23. /// <param name="index">索引</param>
  24. /// <returns></returns>
  25. public string GetKeys(int index)
  26. {
  27. if (index < 0 || index > table.Keys.Count)
  28. throw new Exception("超出索引范围");
  29. string selectN = "";
  30. int i = 0;
  31. foreach (string item in table.Keys)
  32. {
  33. if (index == i)
  34. {
  35. selectN = item; break;
  36. }
  37. i++;
  38. }
  39. return selectN;
  40. }
  41.  
  42. public Product this[int index]
  43. {
  44. get
  45. {
  46. string key = GetKeys(index);
  47. return table[key] as Product;
  48. }
  49. set { string key = GetKeys(index); table[key] = value; }
  50. }
  51. /// <summary>
  52. /// 根据Key值获得对应内容
  53. /// </summary>
  54. /// <param name="Name"></param>
  55. /// <returns></returns>
  56. public string GetKeys(string Name)
  57. {
  58. foreach (string item in table.Keys)
  59. {
  60. if (item==Name)
  61. {
  62. return item;
  63. }
  64. }
  65. throw new Exception("不存在此键值");
  66. }
  67.  
  68. public Product this[string Name]
  69. {
  70. get
  71. {
  72. string selects = GetKeys(Name);
  73. return table[selects] as Product;
  74. }
  75. set
  76. {
  77. string key = GetKeys(Name);
  78. table.Remove(table[key]);
  79. this.Add(value);
  80. }
  81. }
  82.  
  83. /// <summary>
  84. /// 添加功能
  85. /// </summary>
  86. /// <param name="item">添加类</param>
  87. public void Add(Product item)
  88. {
  89. foreach (string key in table.Keys)
  90. {
  91. if(key==item.Code)
  92. throw new Exception("产品代码不能重复");
  93. }
  94. table.Add(item.Code,item);
  95. }
  96.  
  97. /// <summary>
  98. /// 移除功能
  99. /// </summary>
  100. /// <param name="item">添加类</param>
  101. public bool Remove(Product item)
  102. {
  103. try
  104. {
  105. table.Remove(item.Code);
  106. return true;
  107. }
  108. catch
  109. {
  110.  
  111. return false;
  112. }
  113.  
  114. }
  115. /// <summary>
  116. /// 移除指定索引项
  117. /// </summary>
  118. /// <param name="index"></param>
  119. /// <returns></returns>
  120. public bool Remove(int index)
  121. {
  122.  
  123. if (index < 0 || index > table.Count)
  124. throw new Exception("超出索引范围");
  125. string key = GetKeys(index);
  126. table.Remove(key);
  127. return true;
  128. }
  129. /// <summary>
  130. /// 清除所有内容
  131. /// </summary>
  132. public void clear()
  133. {
  134. table = new Hashtable();
  135. }
  136. /// <summary>
  137. /// 获取总数
  138. /// </summary>
  139. public int Count { get { return table.Keys.Count; } }
  140.  
  141. }

  

C# 自定义集合的更多相关文章

  1. 《C#本质论》读书笔记(16)构建自定义集合

    16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...

  2. 使用yield关键字让自定义集合实现foreach遍历

    一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...

  3. UICollectionView(集合视图)以及自定义集合视图

    一.UICollectionView集合视图           其继承自UIScrollView.         UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...

  4. [c#基础]集合foreach的必要条件和自定义集合

    引言 最近翻看了之前的学习笔记,看到foreach,记得当时老师讲的时候,有点犯浑,不是很明白,这好比,上小学时,你不会乘法口诀,但是随着时间的增长,你不自觉的都会了,也悟出个小道理,有些东西,你当时 ...

  5. 集合、拆箱、装箱、自定义集合的foreach

    集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...

  6. 十六、C# 常用集合类及构建自定义集合(使用迭代器)

    常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...

  7. C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能

    1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...

  8. C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能

    IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...

  9. 【VBA编程】10.自定义集合

    自定义集合类型,类似于变量声明,只是要将Dim关键字和New collection关键字搭配起来使用,其语法描述如下:其中集合名的命名方式同于标准变量的命名 Dim 集合名 As New collec ...

  10. 实现自定义集合的可枚举类型(IEnumerable)和枚举数(IEnumerator )

    下面的代码示例演示如何实现自定义集合的 IEnumerable 和 IEnumerator 接口: using System; using System.Collections; using Syst ...

随机推荐

  1. Windows操作 - Photoshop为图片添加透明立体水印

    原文地址:http://design.yesky.com/photoshop/252/2427752.shtml 本文我们介绍用Photoshop为图片添加透明立体水印的方法和技巧. 原图: 打开原图 ...

  2. CCF真题Z型输出

    #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...

  3. mysql创建远程用户

    grant all privileges on *.* to myuser@"%" identified by 'password'; 用root用户登陆,然后: grant al ...

  4. Gradle用户指南(章8:依赖关系管理基础)

    章8:依赖关系管理基础 本章将介绍一些gradle依赖关系管理的基础 什么是依赖关系管理? 简略的说,依赖管理是由两部分组成的.首先,gradle需要知道你要构建或者运行的项目,以便找到它们.我们将这 ...

  5. 慕课网-安卓工程师初养成-4-11 Java循环跳转语句之 break

    来源:http://www.imooc.com/code/1431 生活中,我们经常会因为某些原因中断既定的任务安排.如在参加 10000 米长跑时,才跑了 500 米就由于体力不支,需要退出比赛.在 ...

  6. 【程序与资源】linux程序与资源管理

    程序与资源管理:ps.top.free.sar.kill.uname ①ps语法: [root @test /root ]# ps -aux 参数说明: a   :选择所有的程序列出 u   :所有使 ...

  7. Linux操作系统下三种配置环境变量的方法——转载

    来源:赛迪网 作者:millio       现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以配置java环境变量为例介绍三种配置环境变量的方法. 1.修改/e ...

  8. 非常不错的IT进阶站点

    1:CSDN http://www.csdn.net/ 2:Iteye http://www.iteye.com 3:拼吾爱 http://pin5i.com 4:月光博客 http://www.wi ...

  9. [JFinal 1] JFinal和SSH中使用拦截器的对比

    导读:先前在做.NET项目时,拦截的功能主要是依靠缓存session来实现.当需要跳转到某个页面,使用某个功能查询一些数据时,会根据session中的用户值来判断是否已经正常登录,如果没有,则重定向到 ...

  10. linux设置ulimit值永久生效

    小知识的积累,转自 http://hi.baidu.com/moonelf9989/blog/item/1deadf12780fa0c5c2fd789d.html linux 默认打开文件数linux ...