ID: 1026 Author: Abstractvb.com Date: 4/13/2002 3:08:59 PM C#.NET Adding Objects to a Hashtable Adding objects to a Hashtable is just like adding to a collection since they both inherit from the same parent. Hashtable h = new Hashtable(); h.Add("Key1", "Value1"); h.Add("Key2", "Value2"); h.Add("Key3", "Value3"); h.Add("Key4", "Value4"); h.Add("Key5", "Value5"); Determining if an Object is in the Hashtable You can search the Key and the Value lists in a Hashtable for a specific entry: Here is an example of checking the Key list: Console.WriteLine(h.ContainsKey("Key5")) Here is an example of checking the Value list: Console.WriteLine(h.ContainsValue("Value2")); Reading an Object in the Hashtable To read an object from the Hashtable just use the Item method like so: Console.WriteLine(h.Item("Key3")); Enumerating the Objects in the Hashtable One thing to remember is that the objects may not come out of the HashTable in the same order as you put them in. This is due to the hashing algorithm that keeps things running fast. IDictionaryEnumerator en = h.GetEnumerator(); while (en.MoveNext()) { Console.WriteLine(en.Key + " : " + en.Value); } Want More Source Code? You May Also Be Interested In This: HashTable Tutorial in VB.NET Using HashTables in Visual Basic.NET VB.NET Rank: Difficulty:
2012年11月27日 星期二
hashtable tutorial
Labels:
C#
嗨,大家好!我是一位資訊人、畫畫人、科技人和行銷人。定位自己為網路觀察家,永遠保持好奇心與熱情,學習跨領域新事物,希望最終能成為一個全方位的人。喜歡探索新技術和創意,並將其應用於工作和生活中。也喜歡畫畫、旅行、閱讀和寫作。希望我的部落格可以為您帶來啟發和樂趣,並與您分享我的經驗和見解。謝謝您的訪問,期待與您交流!
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言