Dictionary<TKey,TValue>.Keys Propriedade

Definição

Obtém uma coleção contendo as chaves em .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 de Propriedade

A contendo Dictionary<TKey,TValue>.KeyCollection as chaves no Dictionary<TKey,TValue>.

Exemplos

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

Este 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

Observações

A ordem das chaves em o Dictionary<TKey,TValue>.KeyCollection não é especificada, mas é a mesma ordem dos valores associados em os Dictionary<TKey,TValue>.ValueCollection devolvidos Values pela propriedade.

O retornado Dictionary<TKey,TValue>.KeyCollection não é uma cópia estática; em vez disso, o Dictionary<TKey,TValue>.KeyCollection refere-se às chaves no original Dictionary<TKey,TValue>. Portanto, as alterações ao Dictionary<TKey,TValue> continuam a ser refletidas no Dictionary<TKey,TValue>.KeyCollection.

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

Aplica-se a

Ver também