Custom Types
public class Product {public int Id {get; set;} Self-Increment ID public string Name {get; set;} Name public string Code {get; set;} Primary key public string Category {get; set;} Type public decimal price {get; set;} Price public DateTime producedate {get; set;} Product Time///<summary>///Rewrite ToString Method///</summary>//<returns></retu Rns> public override string ToString () {return String.Format ("{0}{1}{2}{3}{4}{5}", This. Id.tostring (). PadLeft (2), this. Category.padleft (), this. Code.padleft (7), this. Name.padleft (+), this. Price.tostring (). PadLeft (8), this. Producedate.tostring ("yyyy-m-d"). PadLeft (13)); } public static Productcollection getsamplecollection () {productcollection collection = new Prod Uctcollection (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 = "$", 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 = one, Code = "1003", Category = "Red Wine", Name = "Borie Noaillan", Price = 468m, producedate = Dateti Me. Parse ("1995-7-6")}, new Product {Id = $, Code = "1007", Category = "Red Wine", Name = "Pinot Noir Ro" Se ", Price = 710m, Producedate = DateTime.Parse (" 1988-9-10 ")}, new Product {Id = +, Code =" 3009 ", C ategory = "Beer", NamE = "Kingway", Price = 5.5m, Producedate = DateTime.Parse ("2012-6-13")}); return collection; } }
Custom Collection Methods
public class Productcollection {public Hashtable table; Public productcollection () {table = new Hashtable (); } public productcollection (params product[] array) {//Initialize table = new Hashtable (); foreach (Product item in array) {this. ADD (item); }} public ICollection Keys {get {return table. Keys; }}///<summary>///To get the current key value///</summary>//<param name= "index" > Index </param>///<returns></returns> public string getkeys (int index) {if ( Index < 0 | | Index > table. Keys.count) throw new Exception ("Out of Index range"); 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>///for the corresponding content based on key value///</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 ("This key value does not exist"); Public Product this[string Name] {get {string selects = Getkeys (Nam e); Return table[selects] as Product; } set {string key = GetkeYs (Name); Table. Remove (Table[key]); This. ADD (value); }}///<summary>////</summary>//<param name= "Item" > Add Class & lt;/param> public void Add (Product item) {foreach (String key in table. Keys) {if (Key==item. Code) throw new Exception ("Product code cannot be duplicated"); } table. ADD (item. Code,item); }///<summary>/////</summary>//<param name= "Item" > Add Class < ;/param> public bool Remove (Product Item) {try {table. Remove (item. Code); return true; } catch {return false; }}///<summary>//Remove the specified index entry///</summary>//<param name= " Index "></param>// <returns></returns> public bool Remove (int index) {if (Index < 0 | | Index > t Able. Count) throw new Exception ("Out of Index range"); String key = Getkeys (index); Table. Remove (key); return true; }///<summary>///Clear all content///</summary> public void Clear () { Table = new Hashtable (); }///<summary>///Get total///</summary> public int Count {get {return table. Keys.count; } } }
C # Custom Collections