IDictionary.Item[Object] Właściwość

Definicja

Pobiera lub ustawia element przy użyciu określonego klucza.

public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
public object this[object key] { get; set; }
public object? this[object key] { get; set; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

Parametry

key
Object

Klucz elementu do pobrania lub ustawienia.

Wartość właściwości

Element z określonym kluczem lub null jeśli klucz nie istnieje.

Wyjątki

key to null.

Właściwość jest ustawiona IDictionary , a obiekt jest tylko do odczytu.

-lub-

Właściwość jest ustawiona, key nie istnieje w kolekcji i IDictionary ma stały rozmiar.

Przykłady

W poniższym przykładzie kodu pokazano, jak zaimplementować Item[] właściwość. Ten przykład kodu jest częścią większego przykładu podanego IDictionary dla klasy.

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);
            }
        }
    }
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 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

Uwagi

Ta właściwość umożliwia dostęp do określonego elementu w kolekcji przy użyciu następującej składni: myCollection[key].

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w słowniku (na przykład myCollection["myNonexistentKey"] = myValue). Jeśli jednak określony klucz już istnieje w słowniku, ustawienie Item[] właściwości zastępuje starą wartość. Add Natomiast metoda nie modyfikuje istniejących elementów.

Implementacje mogą się różnić w zależności od tego, czy zezwalają na klucz null.

Język C# używa tego słowa kluczowego this do zdefiniowania indeksatorów zamiast implementowania Item[] właściwości. Visual Basic implementuje Item[] jako właściwość domyślną, która zapewnia tę samą funkcjonalność indeksowania.

Dotyczy

Zobacz też