C# 自定义集合

自定义类型

 public class Product
    {
        public int Id { get; set; }        // 自增ID
        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; }        //产品时间

        /// <summary>
        /// 重写ToString 方法
        /// </summary>
        /// <returns></returns>
        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(13));
        }
        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 = 3, Code = "2001", Category = "White Spirit", Name = "Mao Tai", Price = 1680m, ProduceDate = DateTime.Parse("2001-5-8") },
                    new Product { Id = 4, Code = "2013", Category = "White Spirit", Name = "Wu Liang Ye", Price = 1260m, ProduceDate = DateTime.Parse("2005-8-1") },
                    new Product { Id = 8, Code = "3001", Category = "Beer", Name = "TSINGTAO", Price = 6.5m, ProduceDate = DateTime.Parse("2012-4-21") },
                    new Product { Id = 11, Code = "1003", Category = "Red Wine", Name = "Borie Noaillan", Price = 468m, ProduceDate = DateTime.Parse("1995-7-6") },
                    new Product { Id = 15, Code = "1007", Category = "Red Wine", Name = "Pinot Noir Rose", Price = 710m, ProduceDate = DateTime.Parse("1988-9-10") },
                    new Product { Id = 17, Code = "3009", Category = "Beer", Name = "Kingway", Price = 5.5m, ProduceDate = DateTime.Parse("2012-6-13") }
            );
            return collection;
        }
    }

  自定义的集合方法

public class ProductCollection
    {
        public 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; } }

        /// <summary>
        /// 根据索引获取当前Key值
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public string GetKeys(int index)
        {
            if (index < 0 || index > table.Keys.Count)
                throw new Exception("超出索引范围");
            string selectN = "";
            int i = 0;
            foreach (string item in table.Keys)
            {
                if (index == i)
                {
                    selectN = item; break;
                }
                i++;
            }
            return selectN;
        }

        public Product this[int index]
        {
            get
            {
                string key = GetKeys(index);
                return table[key] as Product;
            }
            set { string key = GetKeys(index); table[key] = value; }
        }
        /// <summary>
        /// 根据Key值获得对应内容
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public string GetKeys(string Name)
        {
            foreach (string item in table.Keys)
            {
                if (item==Name)
                {
                    return item;
                }
            }
            throw new Exception("不存在此键值");
        }

        public Product this[string Name]
        {
            get
            {
                string selects = GetKeys(Name);
                return table[selects] as Product;
            }
            set
            {
                string key = GetKeys(Name);
                table.Remove(table[key]);
                this.Add(value);
            }
        }


        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="item">添加类</param>
        public void Add(Product item)
        {
            foreach (string key in table.Keys)
            {
                if(key==item.Code)
                    throw new Exception("产品代码不能重复");
            } 
            table.Add(item.Code,item);
        }
       
        /// <summary>
        /// 移除功能
        /// </summary>
        /// <param name="item">添加类</param>
        public bool Remove(Product item)
        {
            try
            {
                table.Remove(item.Code);
                return true;
            }
            catch 
            {

                return false;
            }
         
        } 
        /// <summary>
        /// 移除指定索引项
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Remove(int index)
        {

            if (index < 0 || index > table.Count)
                throw new Exception("超出索引范围");
            string key = GetKeys(index);
            table.Remove(key);
            return true; 
        }
        /// <summary>
        /// 清除所有内容
        /// </summary>
        public void clear()
        {
            table = new Hashtable();
        }
        /// <summary>
        /// 获取总数
        /// </summary>
        public int Count { get { return table.Keys.Count; } }



    }

  

C# 自定义集合

上一篇:WPF 中模拟键盘和鼠标操作


下一篇:Windows phone 在自定义用户控件(UserControl)——ColorPicker