IDictionary<TKey,TValue>.Add(TKey, TValue) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Adiciona um elemento com a chave e valor fornecidos IDictionary<TKey,TValue>ao .
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)
Parâmetros
- key
- TKey
O objeto a usar como chave do elemento a adicionar.
- value
- TValue
O objeto a usar como valor do elemento a adicionar.
Exceções
key é null.
Um elemento com a mesma chave já existe no IDictionary<TKey,TValue>.
É IDictionary<TKey,TValue> só de leitura.
Exemplos
O exemplo de código seguinte cria um vazio Dictionary<TKey,TValue> de cadeias, com chaves inteiras, e acede-o através da IDictionary<TKey,TValue> interface. O exemplo de código usa o Add método para adicionar alguns elementos. O exemplo demonstra que o Add método lança um ArgumentException ao tentar adicionar uma chave duplicada.
Este código faz parte de um exemplo maior que pode ser compilado e executado. Consulte 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 =
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
Observações
Também pode usar a propriedade Item[] para adicionar novos elementos definindo o valor de uma chave que não existe no dicionário; por exemplo, em C# ( em Visual Basic). No entanto, se a chave especificada já existir no dicionário, definir a Item[] propriedade sobrescreve o valor antigo. Em contraste, o Add método não modifica elementos existentes.
As implementações podem variar na forma como determinam a igualdade dos objetos; Por exemplo, a List<T> classe usa Comparer<T>.Default, enquanto a Dictionary<TKey,TValue> classe permite ao utilizador especificar a IComparer<T> implementação a usar para comparar chaves.
As implementações podem variar quanto a permitir key ser null.