PropertyCollection.IDictionary.Add(Object, Object) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 IDictionary 对象中添加一个带有所提供的键和值的元素。
 virtual void System.Collections.IDictionary.Add(System::Object ^ key, System::Object ^ value) = System::Collections::IDictionary::Add;void IDictionary.Add (object key, object value);abstract member System.Collections.IDictionary.Add : obj * obj -> unit
override this.System.Collections.IDictionary.Add : obj * obj -> unitSub Add (key As Object, value As Object) Implements IDictionary.Add参数
实现
例外
              key 为 null。
IDictionary 对象中已存在具有相同键的元素。
示例
下面的示例演示如何实现 Add 方法。 此代码示例是为 IDictionary 类提供的一个更大示例的一部分。
public:
    virtual void Add(Object^ key, Object^ value)
    {
        // Add the new key/value pair even if this key already exists
        // in the dictionary.
        if (itemsInUse == items->Length)
        {
            throw gcnew InvalidOperationException
                ("The dictionary cannot hold any more items.");
        }
        items[itemsInUse++] = gcnew DictionaryEntry(key, value);
    }
public void Add(object key, object value)
{
    // Add the new key/value pair even if this key already exists in the dictionary.
    if (ItemsInUse == items.Length)
        throw new InvalidOperationException("The dictionary cannot hold any more items.");
    items[ItemsInUse++] = new DictionaryEntry(key, value);
}
Public Sub Add(ByVal key As Object, ByVal value As Object) Implements IDictionary.Add
    ' Add the new key/value pair even if this key already exists in the dictionary.
    If ItemsInUse = items.Length Then
        Throw New InvalidOperationException("The dictionary cannot hold any more items.")
    End If
    items(ItemsInUse) = New DictionaryEntry(key, value)
    ItemsInUse = ItemsInUse + 1
End Sub
注解
还可以使用 Item[] 属性通过设置字典中不存在的键的值来添加新元素 (例如, myCollection["myNonexistentKey"] = myValue) 。 但是,如果字典中已存在指定的键,则设置 Item[] 属性将覆盖旧值。 相反, Add 方法不修改现有元素。