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

Definizione

Ottiene o imposta il valore con la chiave specificata.

C#
object System.Collections.IDictionary.Item[object key] { get; set; }
C#
object? System.Collections.IDictionary.Item[object key] { get; set; }

Parametri

key
Object

Chiave del valore da ottenere.

Valore della proprietà

Valore associato alla chiave specificata o null se key non è presente nel dizionario o se il tipo di key non può essere assegnato al tipo di chiave TKey di Dictionary<TKey,TValue>.

Implementazioni

Eccezioni

key è null.

Viene assegnato un valore e il tipo di key non può essere assegnato al tipo di chiave TKey della classeDictionary<TKey,TValue>.

-oppure-

Viene assegnato un valore e il tipo di value non può essere assegnato al tipo di valore TValue della classeDictionary<TKey,TValue>.

Esempio

Nell'esempio di codice seguente viene illustrato come usare la IDictionary.Item[] proprietà (l'indicizzatore in C#) dell'interfaccia System.Collections.IDictionary con un Dictionary<TKey,TValue>oggetto e come la proprietà differisce dalla Dictionary<TKey,TValue>.Item[] proprietà .

L'esempio mostra che, come la Dictionary<TKey,TValue>.Item[] proprietà , la Dictionary<TKey,TValue>.IDictionary.Item[] proprietà può modificare il valore associato a una chiave esistente e può essere usato per aggiungere una nuova coppia chiave/valore se la chiave specificata non è presente nel dizionario. L'esempio mostra inoltre che, a differenza della Dictionary<TKey,TValue>.Item[] proprietà , la Dictionary<TKey,TValue>.IDictionary.Item[] proprietà non genera un'eccezione se key non è presente nel dizionario, restituendo invece un riferimento Null. L'esempio dimostra infine che il recupero della Dictionary<TKey,TValue>.IDictionary.Item[] proprietà restituisce un riferimento Null se key non è il tipo di dati corretto e tale impostazione genera un'eccezione se key non è il tipo di dati corretto.

L'esempio di codice fa parte di un esempio più ampio, incluso l'output, fornito per il IDictionary.Add metodo .

C#
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");
C#
// 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.");
}
C#
// 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"]);
C#
    }
}

Commenti

Questa proprietà consente di accedere a un valore specifico nella raccolta usando la sintassi C# seguente: myCollection[key] (myCollection(key) in Visual Basic).

È anche possibile utilizzare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nel dizionario, myCollection["myNonexistentKey"] = myValuead esempio . Tuttavia, se la chiave specificata esiste già nel dizionario, l'impostazione della Item[] proprietà sovrascrive il valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.

Il linguaggio C# usa la parola chiave per definire gli indicizzatori anziché implementare la IDictionary.Item[] proprietà . In Visual Basic la proprietà IDictionary.Item[] viene implementata come predefinita per fornire la stessa funzionalità di indicizzazione.

Ottenere o impostare il valore di questa proprietà si avvicina a un'operazione O(1).

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche