Obtiene o establece el elemento con la clave especificada.
Espacio de nombres: System.Collections
Ensamblado: mscorlib (en mscorlib.dll)
Visual Basic (Declaración)
Default Property Item ( _
key As Object _
) As Object
Dim instance As IDictionary
Dim key As Object
Dim value As Object
value = instance(key)
instance(key) = value
Object this [
Object key
] { get; set; }
property Object^ default [Object^] {
Object^ get (Object^ key);
void set (Object^ key, Object^ value);
}
/** @property */
Object get_Item (Object key)
/** @property */
void set_Item (Object key, Object value)
JScript admite el uso de propiedades indizadas, pero no admite la declaración de propiedades nuevas.
Parámetros
- key
Clave del elemento que se va a obtener o establecer.
Valor de propiedad
El elemento con la clave especificada.
| Tipo de excepción | Condición |
|---|
ArgumentNullException |
El valor de key es referencia de objeto null (Nothing en Visual Basic). |
NotSupportedException | La propiedad se establece y el objeto IDictionary es de sólo lectura. O bien La propiedad se establece, key no existe en la colección y el objeto IDictionary tiene un tamaño fijo. |
Esta propiedad permite el acceso a un elemento específico de la colección utilizando la sintaxis siguiente: myCollection[key].
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).
En el ejemplo de código siguiente se muestra cómo implementar la propiedad Item. Este ejemplo de código forma parte de un ejemplo más amplio referente a la clase IDictionary.
Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item
Get
' If this key is in the dictionary, return its value.
Dim index As Integer
If TryGetIndexOfKey(key, index) Then
' The key was found return its value.
Return items(index).Value
Else
' The key was not found return null.
Return Nothing
End If
End Get
Set(ByVal value As Object)
' If this key is in the dictionary, change its value.
Dim index As Integer
If TryGetIndexOfKey(key, index) Then
' The key was found change its value.
items(index).Value = value
Else
' This key is not in the dictionary add this key/value pair.
Add(key, value)
End If
End Set
End Property
public object this[object key]
{
get
{
// If this key is in the dictionary, return its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; return its value.
return items[index].Value;
}
else
{
// The key was not found; return null.
return null;
}
}
set
{
// If this key is in the dictionary, change its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; change its value.
items[index].Value = value;
}
else
{
// This key is not in the dictionary; add this key/value pair.
Add(key, value);
}
}
}
public:
virtual property Object^ default[Object^]
{
Object^ get(Object^ key)
{
// If this key is in the dictionary, return its value.
int index;
if (TryGetIndexOfKey(key, &index))
{
// The key was found; return its value.
return items[index]->Value;
}
else
{
// The key was not found; return null.
return nullptr;
}
}
void set(Object^ key, Object^ value)
{
// If this key is in the dictionary, change its value.
int index;
if (TryGetIndexOfKey(key, &index))
{
// The key was found; change its value.
items[index]->Value = value;
}
else
{
// This key is not in the dictionary; add this
// key/value pair.
Add(key, value);
}
}
}
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.
.NET Framework
Compatible con: 2.0, 1.1, 1.0
.NET Compact Framework
Compatible con: 2.0, 1.0