📌  相关文章
📜  C#|将指定的键和值添加到HybridDictionary

📅  最后修改于: 2021-05-29 17:48:08             🧑  作者: Mango

HybridDictionary.Add(Object,Object)方法用于将具有指定键和值的条目添加到HybridDictionary中。

句法:

public void Add (object key, object value);

参数:

例外情况:

  • ArgumentNullException:如果键为null。
  • ArgumentException:如果HybridDictionary中已经存在具有相同键的条目。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // 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(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

输出:

Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia --> Canberra
Belgium --> Brussels
Netherlands --> Amsterdam
China --> Beijing
Russia --> Moscow
India --> New Delhi

范例2:

// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // 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(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

输出:

Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
I --> first
II --> second
III --> third
IV --> fourth
V --> fifth

笔记:

  • 在其状态与其哈希码值之间不相关的对象通常不应用作键。例如,与用作键的String对象相比,String对象要好于StringBuilder对象。
  • 键不能为null,但值可以为null。
  • 此方法是O(1)操作。
  • 当元素的数量大于ListDictionary的最佳大小时,这些元素将从ListDictionary复制到Hashtable 。但是,这只会发生一次。如果该集合已经存储在Hashtable中,并且元素数低于ListDictionary的最佳大小,则该集合将保留在Hashtable中。

参考:

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