public class Product
{
/// <summary>
/// 自增ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 主键
/// </summary>
public string Code { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 类型
/// </summary>
public string Category { get; set; }
/// <summary>
/// 价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 生产日期
/// </summary>
public DateTime ProduceDate { get; set; }
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}{5}", ID.ToString().PadLeft(), Category.PadLeft(), Code.PadLeft(), Name.PadLeft(), Price.ToString().PadLeft(), ProduceDate.ToString("yyyy-M-d").PadLeft());
}
public static ProductCollection GetSampleCollection()
{
ProductCollection collection = new ProductCollection(
new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product1" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product2" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product3" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product4" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product5" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product6" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product7" }
);
return collection;
}
}

产品类

public class ProductCollection : IEnumerable<Product>
{
private List<Product> list = new List<Product>();
private Hashtable table;
public ProductCollection()
{
table = new Hashtable();
}
public ProductCollection(params Product[] array)
{
table = new Hashtable();
foreach (Product item in array)
{
this.Add(item);
}
}
public ICollection Keys
{
get { return table.Keys; }
}
private string getKey(int index)
{
if (index < || index > table.Keys.Count) throw new Exception("索引超出了范围");
string selected = "";
int i = ;
foreach (string key in table.Keys)
{
if (i == index)
{
selected = key; break;
}
i++;
}
return selected;
}
/// <summary>
/// 索引器 支持类似于collection[index]这样的访问
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Product this[int index]
{
get { string key = getKey(index); return table[key] as Product; }
set { string key = getKey(index); table[key] = value; }
}
private string getKey(string key)
{
foreach (string k in table.Keys)
{
if (key == k)
{
return key;
}
}
throw new Exception("不存在此键值");
}
/// <summary>
/// 索引器 类似于collection[key]这样的访问
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Product this[string key]
{
get { string selected = getKey(key); return table[selected] as Product; }
set { string selected = getKey(key); table.Remove(table[selected]); this.Add(value); }
}
/// <summary>
/// 在末尾添加成员
/// </summary>
/// <param name="item"></param>
public void Add(Product item)
{
//确保key不重复
foreach (string key in table.Keys)
{
if (key == item.Code) throw new Exception("产品代码不能重复");
}
table.Add(item.Code, item);
}
/// <summary>
/// 在任意位置添加成员
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, Product item) { }
/// <summary>
/// 移除某一成员
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(Product item)
{
return true;
}
/// <summary>
/// 移除某一位置的成员
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool RemoveAt(int index) { return true; }
/// <summary>
/// 清除所有成员
/// </summary>
public void Clear() { table.Clear(); }
public IEnumerator<Product> GetEnumerator()
{
return new ProductEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new ProductEnumerator(this);
}
/// <summary>
/// 获得集合的成员数量
/// </summary>
public int Count { get { return table.Keys.Count; } }
public class ProductEnumerator : IEnumerator<Product>
{
private ProductCollection collection;
private int index;
public ProductEnumerator(ProductCollection productCollection)
{
this.collection = productCollection;
index = -;
}
public Product Current { get { return collection[index]; } }
object IEnumerator.Current { get { return collection[index]; } }
public void Dispose() { }
public bool MoveNext()
{
index++;
if (index >= collection.Count) return false;
else return true;
}
public void Reset() { index = -; }
}
}

for和foreach都可进行的迭代

ProductCollection pc = Product.GetSampleCollection();
//for (int i = 0; i < pc.Count-1; i++)
//{
// string line = pc[i].ToString();
// Console.WriteLine(line);
//}
foreach (string item in pc.Keys)
{
Console.WriteLine(pc[item]);
}
Console.ReadKey();

测试代码

最新文章

  1. ReactJs笔记
  2. js实现控制日期月份增减
  3. CentOS 6.5 升级 GCC 4.9.3
  4. 【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。
  5. MyBatis知多少(14)分散的数据库系统
  6. 2014年听写VOA50篇
  7. 2013MPD上海6.23 PM 光耀:读心术,用户心理的产品之道
  8. 集成框架jar包的一些选择
  9. redux学习笔记
  10. 对话机器学习大神Yoshua Bengio(下)
  11. libev事件库学习笔记
  12. ssh服务、密钥登陆配置
  13. ROS探索总结(十九)——如何配置机器人的导航功能
  14. codeforces16B
  15. 【原】无脑操作:eclipse创建maven工程时,如何修改默认JDK版本?
  16. 踩坑学习python自动化测试第二天!
  17. 利用 JMetal 实现大规模聚类问题的研究(二) JMetal代码总览
  18. Python特色数据类型(列表)(上)
  19. &#39;react-scripts&#39; is not recognized as an internal or external command
  20. 如何让一个Java新手快速入门?

热门文章

  1. 【Quartz】工作原理
  2. mysql 与sqlser group by
  3. 详细解读KMP模式匹配算法
  4. HAL库定时器
  5. 【Alpha】任务分解与分配
  6. Zynq-7000 FreeRTOS(一)系统移植配置
  7. STM32F103RE引脚功能整理
  8. Web安全之CSRF攻击的防御措施
  9. Guava源码解析之EventBus
  10. PHP之mb_strrpos使用