C#可遍历的集合
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(2), Category.PadLeft(15), Code.PadLeft(7), Name.PadLeft(17), Price.ToString().PadLeft(4), ProduceDate.ToString("yyyy-M-d").PadLeft(13)); } public static ProductCollection GetSampleCollection() { ProductCollection collection = new ProductCollection( new Product() { ID = 1, Code = "1001", Category = "Red Wine", Name = "Product1" } , new Product() { ID = 2, Code = "1002", Category = "Red Wine", Name = "Product2" } , new Product() { ID = 3, Code = "1003", Category = "Red Wine", Name = "Product3" } , new Product() { ID = 4, Code = "1004", Category = "Red Wine", Name = "Product4" } , new Product() { ID = 5, Code = "1005", Category = "Red Wine", Name = "Product5" } , new Product() { ID = 6, Code = "1006", Category = "Red Wine", Name = "Product6" } , new Product() { ID = 7, Code = "1007", 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 < 0 || index > table.Keys.Count) throw new Exception("索引超出了范围"); string selected = ""; int i = 0; 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 = -1; } 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 = -1; } } }
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();