Aracılığıyla paylaş


PropertyCollection.IDictionary.Item[Object] Özellik

Tanım

Belirtilen anahtarla öğesini alır veya ayarlar.

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

Parametreler

key
Object

Alınacak veya ayarlanacağı öğenin anahtarı.

Özellik Değeri

Belirtilen anahtara sahip öğe.

Uygulamalar

Özel durumlar

key, null değeridir.

özelliği ayarlanır ve IDictionary nesne salt okunurdur.

-veya-

özelliği ayarlanır, key koleksiyonda yoktur ve sabit bir boyuta IDictionary sahiptir.

Örnekler

Aşağıdaki örnekte özelliğin nasıl uygulandığı gösterilmektedir Item[] . Bu kod örneği, sınıfı için IDictionary sağlanan daha büyük bir örneğin parçasıdır.

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

Açıklamalar

Bu özellik, aşağıdaki söz dizimini kullanarak koleksiyondaki belirli bir öğeye erişme olanağı sağlar: myCollection[key].

Özelliğini, sözlükte Item[] bulunmayan bir anahtarın değerini ayarlayarak (örneğin, myCollection["myNonexistentKey"] = myValue) yeni öğeler eklemek için de kullanabilirsiniz. Ancak, belirtilen anahtar sözlükte zaten varsa, özelliğini ayarlamak Item[] eski değerin üzerine yazar. Buna karşılık, Add yöntemi mevcut öğeleri değiştirmez.

Şunlara uygulanır

Ayrıca bkz.