C# 泛型字典 Dictionary的使用詳解
本文主要介紹了C# 泛型字典 Dictionary的使用詳解,分享給大家,具體如下:
泛型最常見的用途是泛型集合,命名空間System.Collections.Generic 中包含了一些基于泛型的集合類,使用泛型集合類可以提供更高的類型安全性,還有更高的性能,避免了非泛型集合的重復的裝箱和拆箱。
很多非泛型集合類都有對應的泛型集合類,我覺得最好還是養(yǎng)成用泛型集合類的好習慣,他不但性能上好而且 功能上要比非泛型類更齊全。下面是常用的非泛型集合類以及對應的泛型集合類
非泛型集合類 | 泛型集合類 |
ArrayList | List<T> |
HashTable | DIctionary<T> |
Queue | Queue<T> |
Stack | Stack<T> |
SortedList | SortedList<T> |
我們用的比較多的非泛型集合類主要有 ArrayList類 和 HashTable類,其中當我們經(jīng)常性的操作 數(shù)據(jù)信息時往往用HashTable 來存儲將要寫入到數(shù)據(jù)庫或者返回的信息,在這之間要不斷的進行類型的轉化,他給我們的幫助應該是非常大的,如果我們操縱的數(shù)據(jù)類型相對確定的化 用Dictionary<TKey,TValue>集合類來存儲數(shù)據(jù)就方便多了,例如我們需要在電子商務網(wǎng)站中存儲用戶的購物車信息( 商品名,對應的商品個數(shù))時,完全可以用Dictionary<string,int > 來存儲購物車信息,而不需要任何的類型轉化。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace L_Dictionary { class Program { static void printDict(Dictionary<int, string> dict) { if(dict.Count == 0) { Console.WriteLine("--沒有數(shù)據(jù)"); return; } else { Console.WriteLine("--打印數(shù)據(jù)"); } foreach (KeyValuePair<int, string> item in dict) { Console.WriteLine("Key: " + item.Key + " Value: " + item.Value); } } static void Main(string[] args) { Console.WriteLine("Dictionary 的基本使用"); #region 本質(zhì) // 擁有Hashtable的泛型 // 以鍵、值對的方式存儲 字典(鍵不能重復) #endregion #region 申明 // 需要引用命名空間 // using System.Collections.Generic; Dictionary<int, string> dict = new Dictionary<int, string>(); #endregion #region 增 Console.WriteLine("-----------------------增"); // 鍵值 不能重復 dict.Add(1, "123"); dict.Add(2, "234"); dict.Add(3, "345"); //dict.Add(3, "345"); // 報錯 printDict(dict); #endregion #region 刪除 Console.WriteLine("-----------------------刪除"); // 只能通過鍵去刪除 // 刪除不存在的 鍵, 沒有反應。 Console.WriteLine("刪除鍵 --- 1"); dict.Remove(1); Console.WriteLine("刪除鍵 --- 4"); dict.Remove(4); printDict(dict); // 清空 Console.WriteLine("清空 ----"); dict.Clear(); printDict(dict); dict.Add(1, "123"); dict.Add(2, "234"); dict.Add(3, "345"); #endregion #region 查詢 Console.WriteLine("-----------------------查詢"); // 1.通過鍵找到 對應的值 // 如果鍵不存在 報錯! Console.WriteLine("查詢鍵2: " + dict[2]); // Console.WriteLine(dict[4]); // 報錯 Console.WriteLine("查詢鍵1: " + dict[1]); // 2.查詢是否存在 // 根據(jù)鍵查詢 if (dict.ContainsKey(1)) { Console.WriteLine("查詢 存在鍵值 1的元素"); } // 根據(jù)值檢測 if (dict.ContainsValue("345")) { Console.WriteLine("查詢 存在\"345 \"值的元素"); } #endregion #region 改 Console.WriteLine("-----------------------改"); Console.WriteLine("修改 鍵=1 元素 值= \"666\" "); dict[1] = "666"; printDict(dict); #endregion #region 遍歷 Console.WriteLine("-----------------------遍歷"); // 1.遍歷所有鍵 Console.WriteLine("---遍歷鍵"); foreach (int item in dict.Keys) { Console.WriteLine(item); } // 2.遍歷所有值 Console.WriteLine("---遍歷所有值"); foreach (string item in dict.Values) { Console.WriteLine(item); } // 3.遍歷所有鍵值 Console.WriteLine("---遍歷所有鍵值"); foreach (KeyValuePair<int, string> item in dict) { Console.WriteLine("Key: " + item.Key + " Value: " + item.Value); } #endregion Console.ReadLine(); } } }
到此這篇關于C# 泛型字典 Dictionary的使用詳解的文章就介紹到這了,更多相關C# 泛型字典 Dictionary內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#實現(xiàn)SQL批量插入數(shù)據(jù)到表的方法
這篇文章主要介紹了C#實現(xiàn)SQL批量插入數(shù)據(jù)到表的方法,涉及C#批量操作SQL的相關技巧,需要的朋友可以參考下2016-04-04C#實現(xiàn)通過程序自動抓取遠程Web網(wǎng)頁信息的代碼
C#實現(xiàn)通過程序自動抓取遠程Web網(wǎng)頁信息的代碼...2007-04-04C#中使用JSON.NET實現(xiàn)JSON、XML相互轉換
這篇文章主要介紹了C#中使用JSON.NET實現(xiàn)JSON、XML相互轉換的相關代碼及示例,需要的朋友可以參考下2015-11-11