專題圖: 編號:ylbtech DotNet100010011
1,IDictionary Interface |
Represents a nongeneric collection of key/value pairs.
【代表一個非泛型鍵/值對的集合。】
命名空間:System.Collections.Generic
程序集:mscorlib(在 mscorlib.dll 中)
2,Syntax(語法) |
public interface IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IEnumerable
3,備注: |
IDictionary 接口是鍵/值對的泛型集合的基接口。
每個元素都是一個存儲在 KeyValuePair 對象中的鍵/值對。
每一對都必須有唯一的鍵。實現在是否允許 key 為 空引用(在 Visual Basic 中為 Nothing) 方面有所不同。此值可以為 空引用(在 Visual Basic 中為 Nothing),並且不必是唯一的。IDictionary 接口允許對所包含的鍵和值進行枚舉,但這並不意味着任何特定的排序順序。
C# 語言中的 foreach 語句(在 Visual Basic 中為 For Each,在 C++ 中為 for each)需要集合中每個元素的類型。由於 IDictionary 的每個元素都是一個鍵/值對,因此元素類型既不是鍵的類型,也不是值的類型。而是 KeyValuePair 類型。
引用: http://msdn.microsoft.com/en-us/library/system.collections.idictionary.aspx
4,統計一句話中,每個單詞的數量【示例】 |
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { /// <summary> /// 統計一句話中,每個單詞的數量 /// </summary> /// <param name="args"></param> static void Main(string[] args) { //創建計數,初始值為0 int count = 0; //創建數據詞典 IDictionary<string, int> dic1 = new Dictionary<string, int>(); //單詞數組 string[] strs = "apler blue hit hit blue".Split(' '); //循環數組,統計每個單詞的數量 for (int i = 0; i < strs.Length; i++) { if (dic1.TryGetValue(strs[i], out count)) { dic1[strs[i]] = count + 1; //存在,在基數上加1 } else { dic1.Add(strs[i], 1); //不存在,添加一個新鍵值對 } } //輸出統計結果 foreach (string key in dic1.Keys) { Console.WriteLine(string.Format("這個單詞{0}, 共{1}。", key, dic1[key])); } } } }
5,把數據詞典綁定集合(Repeater) |
5.1,X.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <select id="Select1"> <asp:Repeater ID="rpt" runat="server"> <ItemTemplate> <option value="<%#Eval("Key") %>"><%#Eval("Value") %></option> </ItemTemplate> </asp:Repeater> </select> </div> </form> </body> </html>
5.2,X.aspx.cs
using System; using System.Collections.Generic; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Dictionary<int, string> flagStateDic = new Dictionary<int, string>(); flagStateDic.Add(0, "保存"); flagStateDic.Add(1, "收件箱"); flagStateDic.Add(2, "草稿箱"); flagStateDic.Add(3, "已發送"); flagStateDic.Add(4, "已刪除"); flagStateDic.Add(18, "訂閱郵件"); flagStateDic.Add(5, "垃圾郵件"); flagStateDic.Add(6, "病毒文件夾"); flagStateDic.Add(7, "廣告郵件"); rpt.DataSource = flagStateDic; rpt.DataBind(); } }
![]() |
作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |
最終目標 |
“代碼的國際化標准示例 ylb,tech”,最大程度地規范軟件編程開發統一,優質, 高效,易學,為建設軟件強國(中國)而努力。