SortedList<TKey,TValue>.Keys Propriedade

Definição

Obtém uma coleção que contém as chaves no SortedList<TKey,TValue>, na ordem de classificação.

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)

Valor da propriedade

IList<TKey>

Um IList<T> que contém as chaves no SortedList<TKey,TValue>.

Exemplos

O exemplo de código a seguir mostra como enumerar as chaves na lista classificada usando a Keys propriedade e como enumerar as chaves e os valores na lista classificada.

O exemplo também mostra como usar a Keys propriedade para uma recuperação indexada eficiente de chaves.

Esse código faz parte de um exemplo maior que pode ser compilado e executado. Consulte 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();
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}"

Comentários

A ordem das chaves no IList<T> é a mesma que a ordem no SortedList<TKey,TValue>.

O retornado IList<T> não é uma cópia estática; em vez disso, o IList<T> faz referência às chaves no original SortedList<TKey,TValue>. Portanto, as alterações no SortedList<TKey,TValue> continuam a ser refletidas no IList<T>.

A coleção retornada pela Keys propriedade fornece uma maneira eficiente de recuperar chaves por índice. Não é necessário regenerar a lista quando a propriedade é acessada, pois a lista é apenas um wrapper para a matriz interna de chaves. O código a seguir mostra o uso da Keys propriedade para recuperação indexada de chaves de uma lista classificada de elementos com chaves de cadeia de caracteres:

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

A recuperação do valor dessa propriedade é uma operação O(1).

Aplica-se a

Confira também