SortedList<TKey,TValue>.Keys Właściwość

Definicja

Pobiera kolekcję zawierającą klucze w obiekcie w SortedList<TKey,TValue>kolejności posortowanej.

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)

Wartość właściwości

IList<TKey>

Element IList<T> zawierający klucze w obiekcie SortedList<TKey,TValue>.

Przykłady

Poniższy przykład kodu przedstawia sposób wyliczania kluczy na posortowanej liście przy użyciu Keys właściwości oraz wyliczania kluczy i wartości na posortowanej liście.

W przykładzie pokazano również, jak używać Keys właściwości do wydajnego indeksowania pobierania kluczy.

Ten kod jest częścią większego przykładu, który można skompilować i wykonać. Zobacz: .

// 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();
for each( 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.
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();
for each( 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();
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}"

Uwagi

Kolejność kluczy w obiekcie IList<T> jest taka sama jak kolejność w pliku SortedList<TKey,TValue>.

Zwrócona IList<T> kopia nie jest kopią statyczną. Zamiast tego IList<T> element odwołuje się do kluczy w oryginalnym SortedList<TKey,TValue>pliku . W związku z SortedList<TKey,TValue> tym zmiany w obiekcie nadal będą odzwierciedlane w pliku IList<T>.

Kolekcja zwrócona Keys przez właściwość zapewnia wydajny sposób pobierania kluczy według indeksu. Ponowne generowanie listy podczas uzyskiwania dostępu do właściwości nie jest konieczne, ponieważ lista jest tylko otoką dla wewnętrznej tablicy kluczy. Poniższy kod przedstawia użycie Keys właściwości do indeksowanego pobierania kluczy z posortowanej listy elementów z kluczami ciągu:

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

Pobieranie wartości tej właściwości jest operacją O(1).

Dotyczy

Zobacz też