集合包括数组和列表
数组:stirng[], int[]
列表:List<T>,ArrayList
集合的特点:
1.可以通过索引或键来访问集合的成员,例如collection[index]或collection[key]
2.可以使用for,foreach循环进行遍历
3.具有属性和方法,用于获得集合中的成员变量,例如常见1的Count属性
4.拥有一些添加、移除成员的方法,例如Add()、Insert() Remove() Clear()
在集合内部,任然是需要一种数据结构来保存数据,传统的做法是使用数组或者链表,使用Hashtable
public class ProductCollection :IEnumerable<Product> { private Hashtable table; public ProductCollection() { this.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; } } public 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; } public string getKey(string key) { foreach (string k in table.Keys) { if (key == k) { return key; } } throw new Exception("不存在此键值!"); } public Product this[int index] { get { string key = getKey(index); return table[key] as Product; } set { string key = getKey(index); table[key] = value; } } 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); } } public void Add(Product item) { foreach (string key in table.Keys) { if (key == item.Code) { throw new Exception("产品代码不能重复"); } } table.Add(item.Code, item); } public void Clear() { table.Clear(); } public int Count { get { return table.Keys.Count; } } public bool Remove(Product item) { return true; } public IEnumerator<Product> GetEnumerator() { return new ConsoleApplication3.Product.ProductEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new ConsoleApplication3.Product.ProductEnumerator(this); } }
public class Product { public int Id { get; set; } public string Name { get; set; } public string Code { get; set; } public string Category { get; set; } public decimal Price { get; set; } public DateTime ProduceDate { get; set; } public override string ToString() { return string.Format("{0}{1}{2}{3}{4}{5}", this.Id.ToString().PadLeft(2), this.Category.PadLeft(15), this.Code.PadLeft(7), this.Name.PadLeft(17), this.Price.ToString().PadLeft(8), this.ProduceDate.ToString("yyyy-M-d").PadLeft(12)); } public static ProductCollection GetSampleCollection() { ProductCollection collection = new ProductCollection( new Product{Id=1,Code="1001",Category="Red Wine",Name="Torres Coronas",Price=285.5m,ProduceDate=DateTime.Parse("1997-12-8")}, new Product{Id=2,Code="2001",Category="White Spirit",Name="Mao tai",Price=1980m,ProduceDate=DateTime.Parse("1998-12-8")}, new Product { Id = 3, Code = "3001", Category = "WuLiangYe", Name = "Torres Coronas", Price = 6.5m, ProduceDate = DateTime.Parse("1899-12-8") }, new Product { Id = 4, Code = "4001", Category = "Beer", Name = "WuLiangYe", Price = 230.5m, ProduceDate = DateTime.Parse("1333-12-8") }, new Product{Id=5,Code="5001",Category="Red Wine",Name="Torres Coronas",Price=299.5m,ProduceDate=DateTime.Parse("1237-01-8")} ); return collection; } public class ProductEnumerator : IEnumerator<Product> { private ProductCollection collection; private int index; public ProductEnumerator(ProductCollection col) { this.collection = col; index = -1; } public Product Current { get { return collection[index]; } } public void Dispose() { } object System.Collections.IEnumerator.Current { get { return collection[index]; } } public bool MoveNext() { index++; if (index >= collection.Count) { return false; } else { return true; } } public void Reset() { index = -1; } } }
foreach (string key in col.Keys) { string line = col[key].ToString(); Console.WriteLine(line); } Console.Read();
上面的代码将ProductEnumerator实现在了Product类中
Enumerator(枚举器)是实现foreach循环的基础,不用for和foreach循环一样可以遍历集合
ProductCollection col = Product.GetSampleCollection(); IEnumerator<Product> e = col.GetEnumerator(); while (e.MoveNext()) { string line = e.Current.ToString(); Console.WriteLine(line); }
所有实现了IEnumerable<T>的对象,都可以遍历,这种叫做序列
序列仅仅关心是否可以遍历,而不关心添加、删除元素