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

Definisi

Menambahkan elemen dengan kunci dan nilai yang disediakan ke IDictionary<TKey,TValue>.

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

Parameter

key
TKey

Objek yang digunakan sebagai kunci elemen untuk ditambahkan.

value
TValue

Objek yang digunakan sebagai nilai elemen untuk ditambahkan.

Pengecualian

keyadalah null.

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

Contoh

Contoh kode berikut membuat string Dictionary<TKey,TValue> kosong, dengan kunci bilangan bulat, dan mengaksesnya melalui IDictionary<TKey,TValue> antarmuka. Contoh kode menggunakan Add metode untuk menambahkan beberapa elemen. Contoh menunjukkan bahwa Add metode melempar ArgumentException saat mencoba menambahkan kunci duplikat.

Kode ini adalah bagian dari contoh yang lebih besar yang dapat dikompilasi dan dijalankan. Lihat System.Collections.Generic.IDictionary<TKey,TValue>.

// Create a new dictionary of strings, with string keys,
// and access it through the IDictionary generic interface.
IDictionary<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,
// and access it through the IDictionary generic interface.
IDictionary<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, 
' and access it through the IDictionary generic interface.
Dim openWith As IDictionary(Of String, String) = _
    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 dalam kamus; misalnya, myCollection["myNonexistentKey"] = myValue di C# (myCollection("myNonexistentKey") = myValue di Visual Basic). Namun, jika kunci yang ditentukan sudah ada dalam kamus, mengatur Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode tidak memodifikasi elemen yang ada.

Implementasi dapat bervariasi dalam cara menentukan kesetaraan objek; misalnya, List<T> kelas menggunakan Comparer<T>.Default, sedangkan Dictionary<TKey,TValue> kelas memungkinkan pengguna untuk menentukan implementasi yang IComparer<T> akan digunakan untuk membandingkan kunci.

Implementasi dapat bervariasi dalam apakah memungkinkan key untuk menjadi null.

Berlaku untuk

Lihat juga