Dictionary<TKey,TValue>.IDictionary.Item[Object] Properti

Definisi

Mendapatkan atau mengatur nilai dengan kunci yang ditentukan.

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

Parameter

key
Object

Kunci nilai yang akan didapatkan.

Nilai Properti

Nilai yang terkait dengan kunci yang ditentukan, atau null jika key tidak ada dalam kamus atau key berjenis yang tidak dapat ditetapkan ke jenis TKeyDictionary<TKey,TValue>kunci .

Penerapan

Pengecualian

keyadalah null.

Nilai sedang ditetapkan, dan key merupakan jenis yang tidak dapat ditetapkan ke jenis TKeyDictionary<TKey,TValue>kunci .

-atau-

Nilai sedang ditetapkan, dan value merupakan jenis yang tidak dapat ditetapkan ke jenis TValueDictionary<TKey,TValue>nilai .

Contoh

Contoh kode berikut menunjukkan cara menggunakan IDictionary.Item[] properti (pengindeks di C#) System.Collections.IDictionary antarmuka dengan Dictionary<TKey,TValue>, dan cara properti berbeda dari Dictionary<TKey,TValue>.Item[] properti .

Contoh menunjukkan bahwa, seperti Dictionary<TKey,TValue>.Item[] properti , Dictionary<TKey,TValue>.IDictionary.Item[] properti dapat mengubah nilai yang terkait dengan kunci yang ada dan dapat digunakan untuk menambahkan pasangan kunci/nilai baru jika kunci yang ditentukan tidak ada dalam kamus. Contohnya juga menunjukkan bahwa tidak seperti Dictionary<TKey,TValue>.Item[] properti , Dictionary<TKey,TValue>.IDictionary.Item[] properti tidak memberikan pengecualian jika key tidak ada dalam kamus, mengembalikan referensi null sebagai gantinya. Terakhir, contoh menunjukkan bahwa mendapatkan Dictionary<TKey,TValue>.IDictionary.Item[] properti mengembalikan referensi null jika key bukan jenis data yang benar, dan pengaturan tersebut menampilkan pengecualian jika key bukan jenis data yang benar.

Contoh kode adalah bagian dari contoh yang lebih besar, termasuk output, yang disediakan untuk metode .IDictionary.Add

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new Dictionary<string, string>();

        // Add some elements to the dictionary. There are no
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.",
    openWith["rtf"]);

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.",
    openWith["rtf"]);

// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";

// The indexer returns null if the key is of the wrong data
// type.
Console.WriteLine("The indexer returns null"
    + " if the key is of the wrong type:");
Console.WriteLine("For key = 2, value = {0}.",
    openWith[2]);

// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
{
    openWith[2] = "This does not get added.";
}
catch (ArgumentException)
{
    Console.WriteLine("A key of the wrong type was specified"
        + " when assigning to the indexer.");
}
' The Item property is the default property, so you 
' can omit its name when accessing elements. 
Console.WriteLine("For key = ""rtf"", value = {0}.", _
    openWith("rtf"))

' The default Item property can be used to change the value
' associated with a key.
openWith("rtf") = "winword.exe"
Console.WriteLine("For key = ""rtf"", value = {0}.", _
    openWith("rtf"))

' If a key does not exist, setting the default Item property
' for that key adds a new key/value pair.
openWith("doc") = "winword.exe"

' The default Item property returns Nothing if the key
' is of the wrong data type.
Console.WriteLine("The default Item property returns Nothing" _
    & " if the key is of the wrong type:")
Console.WriteLine("For key = 2, value = {0}.", _
    openWith(2))

' The default Item property throws an exception when setting
' a value if the key is of the wrong data type.
Try
    openWith(2) = "This does not get added."
Catch 
    Console.WriteLine("A key of the wrong type was specified" _
        & " when setting the default Item property.")
End Try
// Unlike the default Item property on the Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
' Unlike the default Item property on the Dictionary class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the dictionary.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

Keterangan

Properti ini menyediakan kemampuan untuk mengakses nilai tertentu dalam koleksi dengan menggunakan sintaks C# berikut: myCollection[key] (myCollection(key) dalam Visual Basic).

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.

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

Mendapatkan atau mengatur nilai properti ini mendekati operasi O(1).

Berlaku untuk

Lihat juga