SortedList<TKey,TValue>.Keys Egenskap

Definition

Hämtar en samling som innehåller nycklarna i SortedList<TKey,TValue>sorterad ordning.

public:
 property System::Collections::Generic::IList<TKey> ^ Keys { System::Collections::Generic::IList<TKey> ^ get(); };
public System.Collections.Generic.IList<TKey> Keys { get; }
member this.Keys : System.Collections.Generic.IList<'Key>
Public ReadOnly Property Keys As IList(Of TKey)

Egenskapsvärde

IList<TKey>

En IList<T> som innehåller nycklarna i SortedList<TKey,TValue>.

Exempel

I följande kodexempel visas hur du räknar upp nycklarna i den sorterade listan med egenskapen Keys och hur du räknar upp nycklar och värden i den sorterade listan.

Exemplet visar också hur du använder Keys egenskapen för effektiv indexerad hämtning av nycklar.

Den här koden är en del av ett större exempel som kan kompileras och köras. Se även SortedList<TKey,TValue>.

// To get the keys alone, use the Keys property.
IList<string> ilistKeys = openWith.Keys;

// The elements of the list are strongly typed with the
// type that was specified for the SortedList keys.
Console.WriteLine();
foreach( string s in ilistKeys )
{
    Console.WriteLine("Key = {0}", s);
}

// The Keys property is an efficient way to retrieve
// keys by index.
Console.WriteLine("\nIndexed retrieval using the Keys " +
    "property: Keys[2] = {0}", openWith.Keys[2]);
' To get the keys alone, use the Keys property.
Dim ilistKeys As IList(Of String) = openWith.Keys

' The elements of the list are strongly typed with the
' type that was specified for the SortedList keys.
Console.WriteLine()
For Each s As String In ilistKeys 
    Console.WriteLine("Key = {0}", s)
Next s

' The Keys property is an efficient way to retrieve
' keys by index.
Console.WriteLine(vbLf & "Indexed retrieval using the " & _
    "Keys property: Keys(2) = {0}", openWith.Keys(2))
// To get the keys alone, use the Keys property.
let ilistKeys = openWith.Keys;

// The elements of the list are strongly typed with the
// type that was specified for the SortedList keys.
Console.WriteLine()
for s in ilistKeys do
    printfn $"Key = {s}"

// The Keys property is an efficient way to retrieve
// keys by index.
printf "\nIndexed retrieval using the Keys "
printfn $"property: Keys[2] = {openWith.Keys[2]}"
// When you use foreach to enumerate list elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
' When you use foreach to enumerate list elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp
// When you use foreach to enumerate list elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
for kvp in openWith do
    printfn $"Key = {kvp.Key}, Value = {kvp.Value}"

Kommentarer

Ordningen på nycklarna i IList<T> är samma som ordningen i SortedList<TKey,TValue>.

Den returnerade IList<T> är inte en statisk kopia. I stället IList<T> refererar den tillbaka till nycklarna i den ursprungliga SortedList<TKey,TValue>. Därför fortsätter ändringarna att SortedList<TKey,TValue> återspeglas i IList<T>.

Samlingen som returneras av egenskapen Keys är ett effektivt sätt att hämta nycklar efter index. Det är inte nödvändigt att återskapa listan när egenskapen används, eftersom listan bara är en omslutning för den interna matrisen med nycklar. Följande kod visar användningen av Keys egenskapen för indexerad hämtning av nycklar från en sorterad lista med element med strängnycklar:

string v = mySortedList.Values[3];
Dim v As String = mySortedList.Values(3)
let v = mySortedList.Values[3]

Att hämta värdet för den här egenskapen är en O(1)-åtgärd.

Gäller för

Se även