zoukankan      html  css  js  c++  java
  • IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用

    专题图:ylbtech-.net 编号: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();
        }
    }
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    最终目标

     代码的国际化标准示例 ylb,tech”,最大程度地规范软件编程开发统一,优质, 高效,易学,为建设软件强国(中国)而努力。

  • 相关阅读:
    delphi安装pngimage控件,不需要安装,只需引用就行
    怎样把PL/SQLDeveloper字体调大
    resource is out of sync with the file
    SecureCRT:[1]SecureCRT配色方案
    安装开发环境注意事项2
    插入排序
    tomcat更改端口序
    tomcat更改端口
    maven添加jar包
    java总结
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2657157.html
Copyright © 2011-2022 走看看