标签:blog io os ar for sp div on log
自定义类型
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; } //产品时间
///
/// 重写ToString 方法
///
///
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; } }
///
/// 根据索引获取当前Key值
///
/// 索引
///
public string GetKeys(int index)
{
if (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; }
}
///
/// 根据Key值获得对应内容
///
///
///
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);
}
}
///
/// 添加功能
///
/// 添加类
public void Add(Product item)
{
foreach (string key in table.Keys)
{
if(key==item.Code)
throw new Exception("产品代码不能重复");
}
table.Add(item.Code,item);
}
///
/// 移除功能
///
/// 添加类
public bool Remove(Product item)
{
try
{
table.Remove(item.Code);
return true;
}
catch
{
return false;
}
}
///
/// 移除指定索引项
///
///
///
public bool Remove(int index)
{
if (index table.Count)
throw new Exception("超出索引范围");
string key = GetKeys(index);
table.Remove(key);
return true;
}
///
/// 清除所有内容
///
public void clear()
{
table = new Hashtable();
}
///
/// 获取总数
///
public int Count { get { return table.Keys.Count; } }
}
C# 自定义集合
标签:blog io os ar for sp div on log
原文地址:http://www.cnblogs.com/xiao-bei/p/4056926.html