SortedList<TKey,TValue>.IDictionary.Item[Object] Egenskap

Definition

Hämtar eller anger elementet med den angivna nyckeln.

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; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
 Property Item(key As Object) As Object Implements IDictionary.Item

Parametrar

key
Object

Nyckeln för det element som ska hämtas eller anges.

Egenskapsvärde

Elementet med den angivna nyckeln, eller null om key det inte finns i ordlistan eller key är av en typ som inte kan tilldelas till nyckeltypen TKeySortedList<TKey,TValue>för .

Implementeringar

Undantag

key är null.

Ett värde tilldelas och key är av en typ som inte kan tilldelas till nyckeltypen TKey för SortedList<TKey,TValue>.

-eller-

Ett värde tilldelas och är av en typ som inte kan tilldelas till värdetypen TValue för SortedList<TKey,TValue>.

Exempel

Följande kodexempel visar hur du använder IDictionary.Item[] egenskapen (indexeraren i C#) System.Collections.IDictionary i gränssnittet med en SortedList<TKey,TValue>, och hur egenskapen skiljer sig från SortedList<TKey,TValue>.Item[] egenskapen.

Exemplet visar att egenskapen, precis som SortedList<TKey,TValue>.Item[] egenskapen, SortedList<TKey,TValue>.IDictionary.Item[] kan ändra värdet som är associerat med en befintlig nyckel och kan användas för att lägga till ett nytt nyckel/värde-par om den angivna nyckeln inte finns i den sorterade listan. Exemplet visar också att egenskapen till SortedList<TKey,TValue>.IDictionary.Item[] skillnad från SortedList<TKey,TValue>.Item[] egenskapen inte utlöser ett undantag om key den inte finns i den sorterade listan, vilket returnerar en null-referens i stället. Slutligen visar exemplet att hämtar SortedList<TKey,TValue>.IDictionary.Item[] egenskapen returnerar en null-referens om key den inte är rätt datatyp, och att inställningen av egenskapen utlöser ett undantag om key det inte är rätt datatyp.

Kodexemplet är en del av ett större exempel, inklusive utdata, som tillhandahålls IDictionary.Add för metoden.

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

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

        // Add some elements to the sorted list. 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 sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String, String)
        
        ' Add some elements to the sorted list. 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 SorteList class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the sorted list.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
' Unlike the default Item property on the SortedList class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the sorted list.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

Kommentarer

Den här egenskapen returnerar null om key är av en typ som inte kan tilldelas till nyckeltypen TKey för SortedList<TKey,TValue>.

Den här egenskapen ger möjlighet att komma åt ett specifikt element i samlingen med hjälp av följande syntax: myCollection[key].

Du kan också använda Item[] egenskapen för att lägga till nya element genom att ange värdet för en nyckel som inte finns i ordlistan, myCollection["myNonexistentKey"] = myValuetill exempel . Men om den angivna nyckeln redan finns i ordlistan skriver egenskapen Item[] över det gamla värdet. Metoden ändrar däremot Add inte befintliga element.

C#-språket använder det här nyckelordet för att definiera indexerarna i stället för att IDictionary.Item[] implementera egenskapen. Visual Basic implementerar IDictionary.Item[] som en standardegenskap, vilket ger samma indexeringsfunktioner.

Att hämta värdet för den här egenskapen är en O(log n) -åtgärd, där n är Count. Att ange egenskapen är en O(log n) -åtgärd om nyckeln redan finns i SortedList<TKey,TValue>. Om nyckeln inte finns i listan är inställningen för egenskapen en O(n)-åtgärd för osorterade data eller O(log n) om det nya elementet läggs till i slutet av listan. Om infogning orsakar en storleksändring är åtgärden O(n).

Gäller för

Se även