Compartilhar via


Dictionary<TKey,TValue>.Keys Propriedade

Definição

Obtém uma coleção que contém as chaves no Dictionary<TKey,TValue>.

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

Valor da propriedade

Uma Dictionary<TKey,TValue>.KeyCollection que contém as chaves no Dictionary<TKey,TValue>.

Exemplos

O exemplo de código a seguir mostra como enumerar as chaves no dicionário usando a Keys propriedade e como enumerar as chaves e os valores no dicionário.

Esse código faz parte de um exemplo maior que pode ser compilado e executado (openWith é o nome do Dicionário usado neste exemplo). Consulte Dictionary<TKey,TValue>.

// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
    openWith.Keys;

// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
{
    Console.WriteLine("Key = {0}", s);
}
// To get the keys alone, use the Keys property.
let keyColl = openWith.Keys

// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
printfn ""

for s in keyColl do
    printfn $"Key = {s}"
' To get the keys alone, use the Keys property.
Dim keyColl As _
    Dictionary(Of String, String).KeyCollection = _
    openWith.Keys

' The elements of the KeyCollection are strongly typed
' with the type that was specified for dictionary keys.
Console.WriteLine()
For Each s As String In  keyColl
    Console.WriteLine("Key = {0}", s)
Next s
// When you use foreach to enumerate dictionary 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 dictionary elements,
// the elements are retrieved as KeyValuePair objects.
printfn ""

for kvp in openWith do
    printfn $"Key = {kvp.Key}, Value = {kvp.Value}"
' When you use foreach to enumerate dictionary 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

Comentários

A ordem das chaves no Dictionary<TKey,TValue>.KeyCollection não é especificada, mas é a mesma ordem que os valores associados na Dictionary<TKey,TValue>.ValueCollection propriedade retornada Values .

O retornado Dictionary<TKey,TValue>.KeyCollection não é uma cópia estática; em vez disso, ele Dictionary<TKey,TValue>.KeyCollection faz referência às chaves no original Dictionary<TKey,TValue>. Portanto, as alterações na continuação Dictionary<TKey,TValue> a serem refletidas no Dictionary<TKey,TValue>.KeyCollection.

Obter o valor dessa propriedade é uma operação O(1).

Aplica-se a

Confira também