📜  C#| Dictionary.Add()方法

📅  最后修改于: 2021-05-29 19:30:37             🧑  作者: Mango

Dictionary .Add()方法用于将指定的键和值添加到字典中。

句法:

public void Add (TKey key, TValue value);

参数:

例外情况:

  • ArgumentNullException :如果键为null。
  • ArgumentException :如果字典中已经存在具有相同键的元素。

下面是说明使用Dictionary .Add()方法的程序

范例1:

// C# code to add the specified key
// and value into 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 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");
  
        // To get count of key/value
        // pairs in myDict
        Console.WriteLine("Total key/value pairs in"+
                    " myDict are : " + myDict.Count);
  
        // Displaying the key/value
        // pairs in myDict
        Console.WriteLine("The key/value pairs"+
                           " in myDict are : ");
  
        foreach(KeyValuePair kvp in myDict)
        {
            Console.WriteLine("Key = {0}, Value = {1}",
                              kvp.Key, kvp.Value);
        }
    }
}
输出:
Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
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

范例2:

// C# code to add the specified 
// key and value into 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 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");
  
        // The Add method throws an
        // exception if the new key is
        // already in the dictionary.
        try {
  
            myDict.Add("Russia", "Moscow");
        }
  
        catch (ArgumentException) {
  
            Console.WriteLine("An element with Key "+
                     "= \"Russia\" already exists.");
        }
    }
}
输出:
An element with Key = "Russia" already exists.

笔记:

  • 键不能为null,但如果TValue是引用类型,则值可以为null。
  • 如果Count小于容量,则此方法将执行O(1)操作。
  • 如果必须增加容量以容纳新元素,则此方法将成为O(n)操作,其中n为Count。

参考:

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