zoukankan      html  css  js  c++  java
  • C#中Dictionary的用法

    1.要使用Dictionary集合,需要导入C#泛型命名空间

     System.Collections.Generic //程序集:mscorlib

     

    2.Dictionary的描述

    从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成

    任何键都必须是唯一的

    键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值

    Key和Value可以是任何类型(string,int,custom class 等) 

     3.Dictionary常用用法:以 key 的类型为 int , value的类型为string 为例

     创建及初始化

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

      添加元素

    myDictionary.Add(1,"C#");
    
    myDictionary.Add(2,"C++");
    
    myDictionary.Add(3,"ASP.NET");
    
    myDictionary.Add(4,"MVC");

    通过Key查找元素

    if(myDictionary.ContainsKey(1))
    
    {
    
      Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
    
     }

    通过KeyValuePair遍历元素

    foreach(KeyValuePair<int,string> kvp in myDictionary)
    
    {
    
        Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);
    
    }

    仅遍历键 Keys 属性

    Dictionary<int,string>.KeyCollection keyCol = myDictionary.Keys;
    
    foreach(intkeyinkeyCol)
    
    {
    
      Console.WriteLine("Key = {0}", key);
    
    }

    仅遍历值 Valus属性

    Dictionary<int,string>.ValueCollection valueCol = myDictionary.Values;
    
    foreach(stringvalueinvalueCol)
    
    {
    
       Console.WriteLine("Value = {0}", value);
    
    }

    通过Remove方法移除指定的键值

    myDictionary.Remove(1);
    
    if(myDictionary.ContainsKey(1))
    
    {
    
      Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
    
    }
    
    else
    
    {
    
        Console.WriteLine("不存在 Key : 1"); 
    
     }

    4.其它常见属性和方法的说明:

      Comparer:          // 获取用于确定字典中的键是否相等的 IEqualityComparer。
    
      Count:                 // 获取包含在 Dictionary中的键/值对的数目。
    
      Item:                    //获取或设置与指定的键相关联的值。
    
      Keys:                  // 获取包含 Dictionary中的键的集合。
    
      Values:               // 获取包含 Dictionary中的值的集合。
    
      Add:                   // 将指定的键和值添加到字典中。
    
      Clear:                  //从 Dictionary中移除所有的键和值。
    
      ContainsKey:      //确定 Dictionary是否包含指定的键。
    
      ContainsValue:   //确定 Dictionary是否包含特定值。             
    
      GetEnumerator: // 返回循环访问 Dictionary的枚举数。
    
      GetType:           //  获取当前实例的 Type。 (从 Object 继承。)
    
      Remove:             //从 Dictionary中移除所指定的键的值。
    
      ToString:             //返回表示当前 Object的 String。 (从 Object 继承。)
    
      TryGetValue:      //获取与指定的键相关联的值。

  • 相关阅读:
    The Python Standard Library
    Python 中的round函数
    Python文件类型
    Python中import的用法
    Python Symbols 各种符号
    python 一行写多个语句
    免费SSL证书(https网站)申请,便宜SSL https证书申请
    元宇宙游戏Axie龙头axs分析
    OLE DB provider "SQLNCLI10" for linked server "x.x.x.x" returned message "No transaction is active.".
    The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "xxx.xxx.xxx.xxx" was unable to begin a distributed transaction.
  • 原文地址:https://www.cnblogs.com/ldyblogs/p/Dictionary.html
Copyright © 2011-2022 走看看