当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。