共用方式為


OrderedDictionary.Add(Object, Object) 方法

定義

將具有指定之索引鍵和值的元素加入至含有最低可用索引的 OrderedDictionary 集合中。

public:
 virtual void Add(System::Object ^ key, System::Object ^ value);
public void Add (object key, object value);
public void Add (object key, object? value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Sub Add (key As Object, value As Object)

參數

key
Object

要加入的項目索引鍵。

value
Object

要加入的項目值。 這個值可以是 null

實作

例外狀況

OrderedDictionary 集合中已存在具有相同索引鍵的項目。

範例

下列程式代碼範例示範集合的 OrderedDictionary 建立和擴展。 此程式代碼是可在 檢視 OrderedDictionary之較大程式碼範例的一部分。

// Creates and initializes a OrderedDictionary.
OrderedDictionary^ myOrderedDictionary = gcnew OrderedDictionary();
myOrderedDictionary->Add("testKey1", "testValue1");
myOrderedDictionary->Add("testKey2", "testValue2");
myOrderedDictionary->Add("keyToDelete", "valueToDelete");
myOrderedDictionary->Add("testKey3", "testValue3");

ICollection^ keyCollection = myOrderedDictionary->Keys;
ICollection^ valueCollection = myOrderedDictionary->Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary->Count);
// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("keyToDelete", "valueToDelete");
myOrderedDictionary.Add("testKey3", "testValue3");

ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
' Creates and initializes a OrderedDictionary.
Dim myOrderedDictionary As New OrderedDictionary()
myOrderedDictionary.Add("testKey1", "testValue1")
myOrderedDictionary.Add("testKey2", "testValue2")
myOrderedDictionary.Add("keyToDelete", "valueToDelete")
myOrderedDictionary.Add("testKey3", "testValue3")

Dim keyCollection As ICollection = myOrderedDictionary.Keys
Dim valueCollection As ICollection = myOrderedDictionary.Values

' Display the contents Imports the key and value collections
DisplayContents( _
    keyCollection, valueCollection, myOrderedDictionary.Count)

備註

索引鍵不能是 null,但值可以是 。

您也可以使用 Item[] 屬性來新增專案,方法是設定集合中 OrderedDictionary 不存在的索引鍵值;不過,如果指定的索引鍵已存在於 中 OrderedDictionary,則設定 Item[] 屬性會覆寫舊的值。 相反地, Add 方法不會修改現有的專案,而是擲回 ArgumentException

適用於