C#中遍歷Hashtable的4種方法
更新時間:2015年06月17日 09:27:13 投稿:junjie
這篇文章主要介紹了C#中遍歷Hashtable的4種方法,本文直接給出實例代碼,需要的朋友可以參考下
直接上代碼,代碼中使用四種方法遍歷Hashtable。
using System; using System.Collections; namespace HashtableExample { class Program { static Hashtable hashtable = new Hashtable(); static void Main(string[] args) { hashtable.Add("first", "Beijing"); hashtable.Add("second", "Shanghai"); hashtable.Add("third", "Hangzhou"); hashtable.Add("forth", "Nanjing"); //遍歷方法一:遍歷哈希表中的鍵 foreach (string key in hashtable.Keys) { Console.WriteLine(hashtable[key]); } Console.WriteLine("--------------------"); //遍歷方法二:遍歷哈希表中的值 foreach(string value in hashtable.Values) { Console.WriteLine(value); } Console.WriteLine("--------------------"); //遍歷方法三:遍歷哈希表中的鍵值 foreach (DictionaryEntry de in hashtable) { Console.WriteLine(de.Value); } Console.WriteLine("--------------------"); //遍歷方法四:遍歷哈希表中的鍵值 IDictionaryEnumerator myEnumerator = hashtable.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine(hashtable[myEnumerator.Key]); } } } }
下面是代碼的運行結(jié)果。
相關(guān)文章
C#常見的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,
本文對C#中常見集合ArrayList,Hashtable,List<T>,Dictionary<K,V>遍歷方法做了簡單的對比和介紹,有需要的朋友可以參考一下。2016-03-03c#中object、var和dynamic的區(qū)別小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中object、var和dynamic的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解析數(shù)字簽名的substring結(jié)構(gòu)(獲取數(shù)字簽名時間)
解析數(shù)字簽名的substring結(jié)構(gòu),大家參考使用吧2013-12-12