C# Dictionary.Keys用法及代码示例

此属性用于获取包含Dictionary中的键的集合。

用法:

public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; }



返回值:它返回一个包含Dictionary中关键字的集合。

以下示例程序旨在说明上面讨论的属性的使用:

范例1:

// C# code to get the keys 
// in the Dictionary 
using System; 
using System.Collections.Generic; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Create a new dictionary of 
        // strings, with string keys. 
        Dictionary<string, string> myDict =  
           new Dictionary<string, string>(); 
  
        // Adding key/value pairs in myDict 
        myDict.Add("Australia", "Canberra"); 
        myDict.Add("Belgium", "Brussels"); 
        myDict.Add("Netherlands", "Amsterdam"); 
        myDict.Add("China", "Beijing"); 
        myDict.Add("Russia", "Moscow"); 
        myDict.Add("India", "New Delhi"); 
  
        // To get count of key/value pairs in myDict 
        Console.WriteLine("Total key/value pairs"+ 
              " in myDict are:" + myDict.Count); 
  
        // To get the keys alone,  
        // use the Keys property. 
        Dictionary<string, string>.KeyCollection keyColl =  
                                              myDict.Keys; 
  
        // The elements of the KeyCollection 
        // are strongly typed with the type  
        // that was specified for dictionary  
        // keys 
        foreach(string s in keyColl) 
        { 
            Console.WriteLine("Key = {0}", s); 
        } 
    } 
}
 
输出:
Total key/value pairs in myDict are:6
Key = Australia
Key = Belgium
Key = Netherlands
Key = China
Key = Russia
Key = India

范例2:

// C# code to get the keys in the Dictionary 
using System; 
using System.Collections.Generic; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Create a new dictionary of 
        // strings, with string keys. 
        Dictionary<int, int> myDict =  
          new Dictionary<int, int>(); 
  
        // Adding key/value pairs in myDict 
        myDict.Add(9, 8); 
        myDict.Add(3, 4); 
        myDict.Add(4, 7); 
        myDict.Add(1, 7); 
  
        // To get count of key/value pairs in myDict 
        Console.WriteLine("Total key/value pairs "+ 
                "in myDict are:" + myDict.Count); 
  
        // To get the keys alone,  
        // use the Keys property. 
        Dictionary<int, int>.KeyCollection keyColl =  
                                       myDict.Keys; 
  
        // The elements of the KeyCollection 
        // are strongly typed with the type  
        // that was specified for dictionary keys. 
        foreach(int s in keyColl) 
        { 
            Console.WriteLine("Key = {0}", s); 
        } 
    } 
}
 
输出:
Total key/value pairs in myDict are:4
Key = 9
Key = 3
Key = 4
Key = 1




Key就是键,value就是值,

我们在很多地方都会用到字典,他的特点就是查找很快,当然比List快。

字典必须包含名空间System.Collection.Generic 

Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 

键必须是唯一的,而值不需要唯一的
键和值都可以是任何类型(比如:string, int, 自定义类型,等等) 

常用属性

    名称    说明
    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         获取与指定的键相关联的值。

原文地址:https://www.cnblogs.com/qinweizhi/p/15365427.html