📜  C#| Dictionary.Item []属性

📅  最后修改于: 2021-05-29 22:43:14             🧑  作者: Mango

此属性用于获取或设置与Dictionary中的指定键关联的值。

句法:

public TValue this[TKey key] { get; set; }

在这里, key是要获取或设置的值的Key。

属性值:这是与指定键关联的值。如果找不到指定的键,则get操作将抛出KeyNotFoundException ,而set操作将使用指定的键创建一个新元素。

例外情况:

  • ArgumentNullException :如果键为null。
  • KeyNotFoundException :如果检索到属性并且在集合中不存在。

例子:

// C# code to get or set the value
// associated with the specified key
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Dictionary named myDict
        Dictionary myDict = new Dictionary();
  
        // 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");
  
        // Displaying the key/value pairs in myDict
        foreach(KeyValuePair k in myDict)
        {
            Console.WriteLine("Key = {0}, Value = {1}",
                              k.Key, k.Value);
        }
  
        // Displaying the value associated
        // with key "Russia"
        Console.Write("\nValue associated with Russia: ");
        Console.Write(myDict["Russia"]);
  
        // Setting the value associated with key "Russia"
        myDict["Russia"] = "Saint Petersburg";
  
        // Displaying the value associated
        // with key "Russia"
        Console.Write("\n\nValue associated with Russia After Setting: ");
        Console.Write(myDict["Russia"]);
  
        // Displaying the value associated
        // with key "India"
        Console.Write("\n\nValue associated with India: ");
        Console.Write(myDict["India"]);
  
        // Setting the value associated with key "India"
        myDict["India"] = "Mumbai";
  
        // Displaying the value associated
        // with key "India"
        Console.Write("\n\nValue associated with India After Setting: ");
        Console.Write(myDict["India"]);
        Console.WriteLine("\n");
  
        // Displaying the key/value pairs in myDict
        foreach(KeyValuePair k1 in myDict)
        {
            Console.WriteLine("Key = {0}, Value = {1}",
                              k1.Key, k1.Value);
        }
    }
}
输出:
Key = Australia, Value = Canberra
Key = Belgium, Value = Brussels
Key = Netherlands, Value = Amsterdam
Key = China, Value = Beijing
Key = Russia, Value = Moscow
Key = India, Value = New Delhi

Value associated with Russia: Moscow

Value associated with Russia After Setting: Saint Petersburg

Value associated with India: New Delhi

Value associated with India After Setting: Mumbai

Key = Australia, Value = Canberra
Key = Belgium, Value = Brussels
Key = Netherlands, Value = Amsterdam
Key = China, Value = Beijing
Key = Russia, Value = Saint Petersburg
Key = India, Value = Mumbai

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.dictionary-2.item?view=netframework-4.7.2