SortedList<TKey,TValue>.Keys Свойство

Определение

Возвращает коллекцию, содержащую ключи в SortedList<TKey,TValue>отсортированного порядке.

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)

Значение свойства

IList<TKey>

Объект, IList<T> содержащий ключи в объекте SortedList<TKey,TValue>.

Примеры

В следующем примере кода показано, как перечислить ключи в отсортированный список с помощью Keys свойства, а также перечисление ключей и значений в отсортированного списка.

В примере также показано, как использовать Keys свойство для эффективного получения индексированных ключей.

Этот код является частью более крупного примера, который можно скомпилировать и выполнить. См. 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}"

Комментарии

Порядок ключей в ней IList<T> совпадает с порядком.SortedList<TKey,TValue>

Возвращенная IList<T> копия не является статической копией, IList<T> а ссылается на ключи в исходном коде SortedList<TKey,TValue>. Таким образом, изменения SortedList<TKey,TValue> в продолжающихся изменениях отражаются в .IList<T>

Коллекция, возвращаемая свойством Keys , обеспечивает эффективный способ извлечения ключей по индексу. При доступе к свойству не требуется повторно создать список, так как список является просто оболочкой для внутреннего массива ключей. В следующем коде показано использование Keys свойства для индексированного извлечения ключей из отсортированного списка элементов со строковыми ключами:

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

Получение значения этого свойства является операцией O(1).

Применяется к

См. также раздел