SortedList<TKey,TValue>.Keys Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém uma coleção contendo as chaves em , SortedList<TKey,TValue>por ordem ordenada.
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 de Propriedade
A contendo IList<T> as chaves no SortedList<TKey,TValue>.
Exemplos
O seguinte exemplo de código mostra como enumerar as chaves na lista ordenada usando a Keys propriedade e como enumerar as chaves e valores na lista ordenada.
O exemplo também mostra como usar a Keys propriedade para uma recuperação eficiente indexada de chaves.
Este 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();
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}"
Observações
A ordem das chaves em o IList<T> é a mesma que a ordem em .SortedList<TKey,TValue>
O retornado IList<T> não é uma cópia estática; em vez disso, o IList<T> refere-se às chaves no original SortedList<TKey,TValue>. Portanto, as alterações ao SortedList<TKey,TValue> continuam a ser refletidas no IList<T>.
A coleção devolvida pela Keys propriedade fornece uma forma eficiente de recuperar chaves por índice. Não é necessário regenerar a lista quando a propriedade é acedida, porque a lista é apenas um wrapper para o array interno de chaves. O código seguinte mostra a utilização da Keys propriedade para a recuperação indexada de chaves a partir de uma lista ordenada de elementos com chaves de string:
string v = mySortedList.Values[3];
Dim v As String = mySortedList.Values(3)
let v = mySortedList.Values[3]
Recuperar o valor desta propriedade é uma operação O(1).