四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2679|回复: 0
打印 上一主题 下一主题

C#中的Dictionary字典类介绍

[复制链接]
  • TA的每日心情
    开心
    2022-6-10 09:59
  • 366

    主题

    741

    帖子

    9649

    积分

    超级版主

    Rank: 8Rank: 8

    积分
    9649
    跳转到指定楼层
    楼主
    发表于 2016-11-4 10:11:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    作者:txw1958
    原文:http://www.cnblogs.com/txw1958/a ... arp-dictionary.html

    说明
        必须包含名空间System.Collection.Generic
        Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
        键必须是唯一的,而值不需要唯一的
        键和值都可以是任何类型(比如:string, int, 自定义类型,等等)
        通过一个键读取一个值的时间是接近O(1)
        键值对之间的偏序可以不定义

    使用方法:

    1.     //定义
    2.     Dictionary<string, string> openWith = new Dictionary<string, string>();
    复制代码

    1.     //添加元素
    2.     openWith.Add("txt", "notepad.exe");
    3.     openWith.Add("bmp", "paint.exe");
    4.     openWith.Add("dib", "paint.exe");
    5.     openWith.Add("rtf", "wordpad.exe");


    6.     //取值
    7.     Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);


    8.     //更改值
    9.     openWith["rtf"] = "winword.exe";
    10.     Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);


    11.     //遍历key
    12.     foreach (string key in openWith.Keys)
    13.     {
    14.         Console.WriteLine("Key = {0}", key);
    15.     }
    复制代码


    1.     //遍历value
    2.     foreach (string value in openWith.Values)
    3.     {
    4.         Console.WriteLine("value = {0}", value);
    5.     }

    6.     //遍历value, Second Method
    7.     Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
    8.     foreach (string s in valueColl)
    9.     {
    10.         Console.WriteLine("Second Method, Value = {0}", s);
    11.     }
    复制代码



    1.     //遍历字典
    2.     foreach (KeyValuePair<string, string> kvp in openWith)
    3.     {
    4.         Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
    5.     }
    复制代码


    1.     //添加存在的元素
    2.     try
    3.     {
    4.         openWith.Add("txt", "winword.exe");
    5.     }
    6.     catch (ArgumentException)
    7.     {
    8.         Console.WriteLine("An element with Key = \"txt\" already exists.");
    9.     }
    复制代码




    1.     //删除元素
    2.     openWith.Remove("doc");
    3.     if (!openWith.ContainsKey("doc"))
    4.     {
    5.         Console.WriteLine("Key \"doc\" is not found.");
    6.     }
    复制代码



    1.     //判断键存在
    2.     if (openWith.ContainsKey("bmp")) // True
    3.     {
    4.         Console.WriteLine("An element with Key = \"bmp\" exists.");
    5.     }
    复制代码


    参数为其它类型

    1.     //参数为其它类型
    2.     Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
    3.     OtherType.Add(1, "1,11,111".Split(','));
    4.     OtherType.Add(2, "2,22,222".Split(','));
    5.     Console.WriteLine(OtherType[1][2]);
    复制代码


    参数为自定义类型

    首先定义类

    1.     class DouCube
    2.     {
    3.         public int Code { get { return _Code; } set { _Code = value; } } private int _Code;
    4.         public string Page { get { return _Page; } set { _Page = value; } } private string _Page;
    5.     }
    复制代码

    然后


    1.     //声明并添加元素
    2.     Dictionary<int, DouCube> MyType = new Dictionary<int, DouCube>();
    3.     for (int i = 1; i <= 9; i++)
    4.     {
    5.         DouCube element = new DouCube();
    6.         element.Code = i * 100;
    7.         element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
    8.         MyType.Add(i, element);
    9.     }
    复制代码

    1.     //遍历元素
    2.     foreach (KeyValuePair<int, DouCube> kvp in MyType)
    3.     {
    4.         Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
    5.     }
    复制代码




    常用属性

        名称    说明
        Comparer     获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
        Count        获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目。
        Item         获取或设置与指定的键相关联的值。
        Keys         获取包含 Dictionary<TKey, TValue> 中的键的集合。
        Values       获取包含 Dictionary<TKey, TValue> 中的值的集合。

    常用方法
        名称    说明
        Add                 将指定的键和值添加到字典中。
        Clear               从 Dictionary<TKey, TValue> 中移除所有的键和值。
        ContainsKey         确定 Dictionary<TKey, TValue> 是否包含指定的键。
        ContainsValue       确定 Dictionary<TKey, TValue> 是否包含特定值。
        Equals(Object)      确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
        Finalize            允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
        GetEnumerator       返回循环访问 Dictionary<TKey, TValue> 的枚举器。
        GetHashCode         用作特定类型的哈希函数。 (继承自 Object。)
        GetObjectData       实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据。
        GetType             获取当前实例的 Type。 (继承自 Object。)
        MemberwiseClone     创建当前 Object 的浅表副本。 (继承自 Object。)
        OnDeserialization    实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
        Remove              从 Dictionary<TKey, TValue> 中移除所指定的键的值。
        ToString            返回表示当前对象的字符串。 (继承自 Object。)
        TryGetValue         获取与指定的键相关联的值。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-7 04:43 , Processed in 1.062500 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表