Biblioteca de clases de .NET Framework
IDictionary.Add (Método)

Agrega un elemento con la clave y el valor proporcionados al objeto IDictionary.

Espacio de nombres: System.Collections
Ensamblado: mscorlib (en mscorlib.dll)

Sintaxis

Visual Basic (Declaración)
Sub Add ( _
    key As Object, _
    value As Object _
)
Visual Basic (Uso)
Dim instance As IDictionary
Dim key As Object
Dim value As Object

instance.Add(key, value)
C#
void Add (
    Object key,
    Object value
)
C++
void Add (
    Object^ key, 
    Object^ value
)
J#
void Add (
    Object key, 
    Object value
)
JScript
function Add (
    key : Object, 
    value : Object
)

Parámetros

key

Object que se va a utilizar como clave del elemento que se va a agregar.

value

Object que se va a utilizar como valor del elemento que se va a agregar.

Excepciones

Tipo de excepciónCondición

ArgumentNullException

El valor de key es referencia de objeto null (Nothing en Visual Basic).

ArgumentException

Ya existe un elemento con la misma clave en el objeto IDictionary.

NotSupportedException

IDictionary es de sólo lectura.

O bien

IDictionary tiene un tamaño fijo.

Comentarios

También puede utilizar la propiedad Item para agregar nuevos elementos estableciendo el valor de una clave que no existe en el diccionario (por ejemplo, myCollection["myNonexistentKey"] = myValue). Sin embargo, si la clave especificada ya existe en el diccionario, al establecer la propiedad Item se reemplaza el valor antiguo. En cambio, el método Add no modifica los elementos existentes.

Las implementaciones pueden variar en si permiten que la clave sea referencia de objeto null (Nothing en Visual Basic).

Ejemplo

En el ejemplo de código siguiente se muestra cómo implementar el método Add. Este ejemplo de código forma parte de un ejemplo más amplio referente a la clase IDictionary.

Visual Basic
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
C#
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);
}
C++
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);
    }
Plataformas

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

Información de versión

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
Vea también

Etiquetas :


Page view tracker