SortedList<TKey,TValue>.Item[TKey] Proprietà

Definizione

Ottiene o imposta il valore associato alla chiave specificata.

public:
 property TValue default[TKey] { TValue get(TKey key); void set(TKey key, TValue value); };
public TValue this[TKey key] { get; set; }
member this.Item('Key) : 'Value with get, set
Default Public Property Item(key As TKey) As TValue

Parametri

key
TKey

Chiave di cui si deve ottenere o impostare il valore.

Valore della proprietà

TValue

Valore associato alla chiave specificata. Se la chiave specificata non viene trovata, un'operazione get genera KeyNotFoundException e un'operazione set crea un nuovo elemento con la chiave specificata.

Implementazioni

Eccezioni

key è null.

La proprietà viene recuperata e key non esiste nella raccolta.

Esempio

Nell'esempio di codice seguente viene usata la Item[] proprietà (indicizzatore in C#) per recuperare i valori, dimostrando che viene generata una KeyNotFoundException chiave quando una chiave richiesta non è presente e che il valore associato a una chiave può essere sostituito.

L'esempio mostra anche come usare il TryGetValue metodo come modo più efficiente per recuperare i valori se un programma spesso deve provare i valori chiave che non si trovano nell'elenco ordinato.

Questo esempio di codice fa parte di un esempio più grande fornito per la SortedList<TKey,TValue> classe.

// 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 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 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 indexer throws an exception if the requested key is
// not in the list.
try
{
    Console::WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException^)
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console.WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException)
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' When a program often has to try keys that turn out not to
' be in the list, TryGetValue can be a more efficient 
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
    Console.WriteLine("Key = ""tif"" is not found.")
End If

Commenti

Questa proprietà consente di accedere a un elemento specifico nella raccolta utilizzando la sintassi seguente: myCollection[key].

Una chiave non può essere , ma un valore può essere null, se il tipo di valori nell'elenco, TValueè un tipo di riferimento.

Se la chiave non viene trovata quando viene recuperato un valore, KeyNotFoundException viene generata. Se la chiave non viene trovata quando viene impostato un valore, viene aggiunta la chiave e il valore.

È anche possibile usare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nell'oggetto SortedList<TKey,TValue>, ad esempio myCollection["myNonexistentKey"] = myValue. Tuttavia, se la chiave specificata esiste già nell'impostazione SortedList<TKey,TValue>, impostando la proprietà sovrascrive il Item[] valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.

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

Il recupero del valore di questa proprietà è un'operazione O(log n), dove n è Count. L'impostazione della proprietà è un'operazione O(log n) se la chiave è già presente in SortedList<TKey,TValue>. Se la chiave non è nell'elenco, l'impostazione della proprietà è un'operazione O() per i dati non arrestati o O(nlog n) se il nuovo elemento viene aggiunto alla fine dell'elenco. Se l'inserimento causa un ridimensionamento, l'operazione è O(n).

Si applica a

Vedi anche