Dictionary<TKey,TValue>.Keys Propiedad

Definición

Obtiene una colección que contiene las claves de 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 propiedad

Dictionary<TKey,TValue>.KeyCollection

Dictionary<TKey,TValue>.KeyCollection que contiene las claves de Dictionary<TKey,TValue>.

Ejemplos

En el ejemplo de código siguiente se muestra cómo enumerar las claves del diccionario mediante la Keys propiedad y cómo enumerar las claves y los valores del diccionario.

Este código forma parte de un ejemplo más grande que se puede compilar y ejecutar (openWith es el nombre del diccionario usado en este ejemplo). Vea 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();
for each( String^ s in keyColl )
{
    Console::WriteLine("Key = {0}", s);
}
// 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.
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();
for each( 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.
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.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp

Comentarios

El orden de las claves de Dictionary<TKey,TValue>.KeyCollection no se especifica, pero es el mismo orden que los valores asociados en el Dictionary<TKey,TValue>.ValueCollection devuelto por la Values propiedad .

El devuelto Dictionary<TKey,TValue>.KeyCollection no es una copia estática; en su lugar, Dictionary<TKey,TValue>.KeyCollection hace referencia a las claves del original Dictionary<TKey,TValue>. Por lo tanto, los cambios en el Dictionary<TKey,TValue> objeto se seguirán reflejando en Dictionary<TKey,TValue>.KeyCollection.

Obtener el valor de esta propiedad es una operación O(1).

Se aplica a

Consulte también