當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# SortedDictionary.Add()用法及代碼示例


這用於將指定的鍵和值添加到已排序的字典中。元素根據TKey排序。

用法:

public void Add (TKey key, TValue value);

參數:


  • key:這是要添加的元素的關鍵。
    value:它是要添加的元素的值。對於引用類型,該值可以為null。

異常:

  • ArgumentNullException:如果鍵為null。
  • ArgumentException:如果字典中已經存在具有相同鍵的元素。

以下示例程序旨在說明Dictionary.Add()方法的使用:

示例1:

// C# code to add the specified key 
// and value into the SortedDictionary 
using System; 
using System.Collections.Generic; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Create a new SortedDictionary 
        // of strings, with string keys. 
        SortedDictionary<string, string> myDict =  
           new SortedDictionary<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); 
  
        // Displaying the key/value 
        // pairs in myDict 
        Console.WriteLine("The key/value pairs"
                          + " in myDict are : "); 
  
        foreach(KeyValuePair<string, string> 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 = China, Value = Beijing
Key = India, Value = New Delhi
Key = Netherlands, Value = Amsterdam
Key = Russia, Value = Moscow

示例2:

// C# code to add the specified key 
// and value into the SortedDictionary 
using System; 
using System.Collections.Generic; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Create a new SortedDictionary 
        // of strings, with string keys. 
        SortedDictionary<string, string> myDict = 
           new SortedDictionary<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"); 
  
        // 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,但值可以為null。如果值類型TValue是引用類型。
  • 此方法是O(log n)操作,其中n是SortedDictionary中的元素計數。

參考:



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 C# | SortedDictionary.Add() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。