Dictionary<TKey,TValue>.Add(TKey, TValue) Metode

Definisi

Menambahkan kunci dan nilai yang ditentukan ke kamus.

public:
 virtual void Add(TKey key, TValue value);
public void Add (TKey key, TValue value);
abstract member Add : 'Key * 'Value -> unit
override this.Add : 'Key * 'Value -> unit
Public Sub Add (key As TKey, value As TValue)

Parameter

key
TKey

Kunci elemen yang akan ditambahkan.

value
TValue

Nilai elemen yang akan ditambahkan. Nilainya bisa null untuk jenis referensi.

Penerapan

Pengecualian

keyadalah null.

Elemen dengan kunci yang sama sudah ada di Dictionary<TKey,TValue>.

Contoh

Contoh kode berikut membuat string kosong Dictionary<TKey,TValue> dengan kunci string dan menggunakan Add metode untuk menambahkan beberapa elemen. Contoh menunjukkan bahwa Add metode melemparkan ArgumentException saat mencoba menambahkan kunci duplikat.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk Dictionary<TKey,TValue> kelas .

// Create a new dictionary of strings, with string keys.
//
Dictionary<String^, String^>^ openWith =
    gcnew Dictionary<String^, String^>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

Keterangan

Anda juga dapat menggunakan Item[] properti untuk menambahkan elemen baru dengan mengatur nilai kunci yang tidak ada di Dictionary<TKey,TValue>; misalnya, myCollection[myKey] = myValue (di Visual Basic, myCollection(myKey) = myValue). Namun, jika kunci yang ditentukan sudah ada di Dictionary<TKey,TValue>, pengaturan Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode melemparkan pengecualian jika nilai dengan kunci yang ditentukan sudah ada.

Count Jika nilai properti sudah sama dengan kapasitas, kapasitas Dictionary<TKey,TValue> ditingkatkan dengan secara otomatis merealokasi array internal, dan elemen yang ada disalin ke array baru sebelum elemen baru ditambahkan.

Kunci tidak boleh null, tetapi nilainya bisa, jika TValue merupakan jenis referensi.

Jika Count kurang dari kapasitas, metode ini mendekati operasi O(1). Jika kapasitas harus ditingkatkan untuk mengakomodasi elemen baru, metode ini menjadi operasi O(n), di mana n adalah Count.

Berlaku untuk

Lihat juga