且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IDictionary 接口(数据词典)讲解与示例应用

更新时间:2022-09-25 10:05:57

专题图:IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用 编号: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

IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用
<%@ 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>
IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用

5.2,X.aspx.cs

IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用
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();
    }
}
IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/08/26/2657157.html,如需转载请自行联系原作者