C#中Dictionary的用法及用途實例

一、Dictionary<string, string>是一個泛型


 
他本身有集合的功能有時候可以把它看成數組

他的結構是這樣的:Dictionary<[key], [value]>

他的特點是存入對象是需要與[key]值一一對應的存入該泛型

通過某一個一定的[key]去找到對應的值

舉個例子:
 
//實例化對象

Dictionary<int, string> dic = new Dictionary<int, string>();

//對象打點添加

dic.Add(1, "one");

dic.Add(2, "two");

dic.Add(3, "one");

//提取元素的方法

string a = dic[1];

string b = dic[2];

string c = dic[3];

//1、2、3是鍵,分別對應“one”“two”“one”

//上面代碼中分別把值賦給了a,b,c

//注意,鍵相當於找到對應值的唯一標識,所以不能重複

//但是值可以重複

----------------------------------------------------------------------------------------
 
----------------------------------------------------------------------------------------
 
二、c# 對dictionary類進行排序用什麼接口實現
 
如果使用.Net Framework 3.5的話,事情就很簡單了。呵呵。

如果不是的話,還是自己寫排序吧。

 

  1.  
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Text;    
  5. using System.Linq;  
  6.  
  7. namespace DictionarySorting  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Dictionary<intstring> dic = new Dictionary<intstring>();  
  14.  
  15.             dic.Add(1, "HaHa");  
  16.             dic.Add(5, "HoHo");  
  17.             dic.Add(3, "HeHe");  
  18.             dic.Add(2, "HiHi");  
  19.             dic.Add(4, "HuHu");  
  20.  
  21.             var result = from pair in dic orderby pair.Key select pair;  
  22.  
  23.             foreach (KeyValuePair<intstring> pair in result)  
  24.             {  
  25.                 Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);  
  26.             }  
  27.             Console.ReadKey();  
  28.         }  
  29.     }  
  30. }  
  31.    
  32. 【執行結果】    
  33. Key:1, Value:HaHa    
  34. Key:2, Value:HiHi    
  35. Key:3, Value:HeHe    
  36. Key:4, Value:HuHu    
  37. Key:5, Value:HoHo  

================================================================================
 
三、Dictionary的基本用法。假如
 
需求:現在要導入一批數據,這些數據中有一個稱爲公司的字段是我們數據庫裏已經存在了的,目前我們需要把每個公司名字轉爲ID後才存入數據庫。
 
分析:每導一筆記錄的時候,就把要把公司的名字轉爲公司的ID,這個不應該每次都查詢一下數據庫的,因爲這太耗數據庫的性能了。
 
解決方案:在業務層裏先把所有的公司名稱及相應的公司ID一次性讀取出來,然後存放到一個Key和Value的鍵值對裏,然後實現只要把一個公司的名字傳進去,就可以得到此公司相應的公司ID,就像查字典一樣。對,我們可以使用字典Dictionary操作這些數據。
 
示例:SetKeyValue()方法相應於從數據庫裏讀取到了公司信息。
 
 

  1. /// <summary>   
  2. /// 定義Key爲string類型,Value爲int類型的一個Dictionary   
  3. /// </summary>   
  4. /// <returns></returns>  
  5.    
  6. protected Dictionary<stringint> SetKeyValue()   
  7. {   
  8.     Dictionary<stringint> dic = new Dictionary<stringint>();   
  9.  
  10.     dic.Add("公司1", 1);   
  11.     dic.Add("公司2", 2);   
  12.     dic.Add("公司3", 3);   
  13.     dic.Add("公司4", 4);  
  14.    
  15.     return dic;   
  16. }  
  17.    
  18. /// <summary>   
  19. /// 得到根據指定的Key行到Value   
  20. /// </summary>   
  21. protected void GetKeyValue()   
  22. {   
  23.     Dictionary<stringint> myDictionary = SetKeyValue();  
  24.    
  25.     //測試得到公司2的值   
  26.     int directorValue = myDictionary["公司2"];   
  27.     Response.Write("公司2的value是:" + directorValue.ToString());   
  28. }  
  29.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章