C# 泛型字典 Dictionary的使用詳解
本文主要介紹了C# 泛型字典 Dictionary的使用詳解,分享給大家,具體如下:
泛型最常見的用途是泛型集合,命名空間System.Collections.Generic 中包含了一些基于泛型的集合類,使用泛型集合類可以提供更高的類型安全性,還有更高的性能,避免了非泛型集合的重復(fù)的裝箱和拆箱。
很多非泛型集合類都有對(duì)應(yīng)的泛型集合類,我覺得最好還是養(yǎng)成用泛型集合類的好習(xí)慣,他不但性能上好而且 功能上要比非泛型類更齊全。下面是常用的非泛型集合類以及對(duì)應(yīng)的泛型集合類
非泛型集合類 | 泛型集合類 |
ArrayList | List<T> |
HashTable | DIctionary<T> |
Queue | Queue<T> |
Stack | Stack<T> |
SortedList | SortedList<T> |
我們用的比較多的非泛型集合類主要有 ArrayList類 和 HashTable類,其中當(dāng)我們經(jīng)常性的操作 數(shù)據(jù)信息時(shí)往往用HashTable 來存儲(chǔ)將要寫入到數(shù)據(jù)庫(kù)或者返回的信息,在這之間要不斷的進(jìn)行類型的轉(zhuǎn)化,他給我們的幫助應(yīng)該是非常大的,如果我們操縱的數(shù)據(jù)類型相對(duì)確定的化 用Dictionary<TKey,TValue>集合類來存儲(chǔ)數(shù)據(jù)就方便多了,例如我們需要在電子商務(wù)網(wǎng)站中存儲(chǔ)用戶的購(gòu)物車信息( 商品名,對(duì)應(yīng)的商品個(gè)數(shù))時(shí),完全可以用Dictionary<string,int > 來存儲(chǔ)購(gòu)物車信息,而不需要任何的類型轉(zhuǎn)化。
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的泛型 // 以鍵、值對(duì)的方式存儲(chǔ) 字典(鍵不能重復(fù)) #endregion #region 申明 // 需要引用命名空間 // using System.Collections.Generic; Dictionary<int, string> dict = new Dictionary<int, string>(); #endregion #region 增 Console.WriteLine("-----------------------增"); // 鍵值 不能重復(fù) dict.Add(1, "123"); dict.Add(2, "234"); dict.Add(3, "345"); //dict.Add(3, "345"); // 報(bào)錯(cuò) printDict(dict); #endregion #region 刪除 Console.WriteLine("-----------------------刪除"); // 只能通過鍵去刪除 // 刪除不存在的 鍵, 沒有反應(yīng)。 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.通過鍵找到 對(duì)應(yīng)的值 // 如果鍵不存在 報(bào)錯(cuò)! Console.WriteLine("查詢鍵2: " + dict[2]); // Console.WriteLine(dict[4]); // 報(bào)錯(cuò) Console.WriteLine("查詢鍵1: " + dict[1]); // 2.查詢是否存在 // 根據(jù)鍵查詢 if (dict.ContainsKey(1)) { Console.WriteLine("查詢 存在鍵值 1的元素"); } // 根據(jù)值檢測(cè) 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(); } } }
到此這篇關(guān)于C# 泛型字典 Dictionary的使用詳解的文章就介紹到這了,更多相關(guān)C# 泛型字典 Dictionary內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net頁(yè)面中如何獲取Excel表的內(nèi)容
在瀏覽網(wǎng)頁(yè)時(shí),一定會(huì)看到我們需要保存的信息,我們比較常用的方法就是拖動(dòng)鼠標(biāo),選中我們需要的內(nèi)容,然后ctrl+c,然后在保持到excel當(dāng)中去,那么如何用asp.net獲取excel表的內(nèi)容呢,下面小編就給大家介紹asp.net獲取excel表的內(nèi)容,需要的朋友可以參考下2015-08-08Unity 點(diǎn)擊UI與點(diǎn)擊屏幕沖突的解決方案
這篇文章主要介紹了Unity 點(diǎn)擊UI與點(diǎn)擊屏幕沖突的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#實(shí)現(xiàn)SQL批量插入數(shù)據(jù)到表的方法
這篇文章主要介紹了C#實(shí)現(xiàn)SQL批量插入數(shù)據(jù)到表的方法,涉及C#批量操作SQL的相關(guān)技巧,需要的朋友可以參考下2016-04-04C# wpf簡(jiǎn)單顏色板的實(shí)現(xiàn)
wpf本身沒有提供顏色板之類的控件,有些業(yè)務(wù)使用場(chǎng)景需要使用顏色板之類的控件,本文就簡(jiǎn)單實(shí)現(xiàn),感興趣的可以了解一下2021-10-10C#實(shí)現(xiàn)通過程序自動(dòng)抓取遠(yuǎn)程Web網(wǎng)頁(yè)信息的代碼
C#實(shí)現(xiàn)通過程序自動(dòng)抓取遠(yuǎn)程Web網(wǎng)頁(yè)信息的代碼...2007-04-04C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
這篇文章主要介紹了C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換的相關(guān)代碼及示例,需要的朋友可以參考下2015-11-11