Dictionary<TKey,TValue>.Keys Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar en samling som innehåller nycklarna i 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
Egenskapsvärde
En Dictionary<TKey,TValue>.KeyCollection som innehåller nycklarna i Dictionary<TKey,TValue>.
Exempel
I följande kodexempel visas hur du räknar upp nycklarna i ordlistan med hjälp av Keys egenskapen och hur du räknar upp nycklar och värden i ordlistan.
Den här koden är en del av ett större exempel som kan kompileras och köras (openWith är namnet på ordlistan som används i det här exemplet). Se även 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
Kommentarer
Ordningen på nycklarna i Dictionary<TKey,TValue>.KeyCollection är ospecificerad, men det är samma ordning som de associerade värdena i den Dictionary<TKey,TValue>.ValueCollection som returneras av Values egenskapen.
Den returnerade Dictionary<TKey,TValue>.KeyCollection är inte en statisk kopia. I stället Dictionary<TKey,TValue>.KeyCollection refererar den tillbaka till nycklarna i den ursprungliga Dictionary<TKey,TValue>. Därför fortsätter ändringarna att Dictionary<TKey,TValue> återspeglas i Dictionary<TKey,TValue>.KeyCollection.
Att hämta värdet för den här egenskapen är en O(1)-åtgärd.