IDictionary.Item[Object] Properti

Definisi

Mendapatkan atau mengatur elemen dengan kunci yang ditentukan.

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

Parameter

key
Object

Kunci elemen untuk mendapatkan atau mengatur.

Nilai Properti

Elemen dengan kunci yang ditentukan, atau null jika kunci tidak ada.

Pengecualian

keyadalah null.

Properti diatur dan IDictionary objek bersifat baca-saja.

-atau-

Properti diatur, key tidak ada dalam koleksi, dan IDictionary memiliki ukuran tetap.

Contoh

Contoh kode berikut menunjukkan cara mengimplementasikan Item[] properti . Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk IDictionary kelas .

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

Keterangan

Properti ini menyediakan kemampuan untuk mengakses elemen tertentu dalam koleksi dengan menggunakan sintaks berikut: myCollection[key].

Anda juga dapat menggunakan Item[] properti untuk menambahkan elemen baru dengan mengatur nilai kunci yang tidak ada dalam kamus (misalnya, myCollection["myNonexistentKey"] = myValue). Namun, jika kunci yang ditentukan sudah ada dalam kamus, mengatur Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode tidak memodifikasi elemen yang ada.

Implementasi dapat bervariasi dalam apakah memungkinkan kunci menjadi null.

Bahasa C# menggunakan this kata kunci ini untuk menentukan pengindeks alih-alih mengimplementasikan Item[] properti . Visual Basic mengimplementasikan Item[] sebagai properti default, yang menyediakan fungsionalitas pengindeksan yang sama.

Berlaku untuk

Lihat juga