SortedList<TKey,TValue>.Keys Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u een verzameling op die de sleutels in de SortedList<TKey,TValue>gesorteerde volgorde bevat.
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)
Waarde van eigenschap
Een IList<T> met de sleutels in de SortedList<TKey,TValue>.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de sleutels in de gesorteerde lijst opsommen met behulp van de Keys eigenschap en hoe u de sleutels en waarden in de gesorteerde lijst opsommen.
In het voorbeeld ziet u ook hoe u de Keys eigenschap gebruikt voor het efficiënt ophalen van geïndexeerde sleutels.
Deze code maakt deel uit van een groter voorbeeld dat kan worden gecompileerd en uitgevoerd. Zie 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}"
Opmerkingen
De volgorde van de sleutels in de IList<T> is hetzelfde als de volgorde in de SortedList<TKey,TValue>.
Het geretourneerde IList<T> is geen statische kopie. In plaats daarvan verwijst de IList<T> waarde terug naar de sleutels in het origineel SortedList<TKey,TValue>. Daarom blijven wijzigingen in de wijzigingen in de SortedList<TKey,TValue>IList<T>.
De verzameling die door de Keys eigenschap wordt geretourneerd, biedt een efficiënte manier om sleutels op index op te halen. Het is niet nodig om de lijst opnieuw te genereren wanneer de eigenschap wordt geopend, omdat de lijst slechts een wrapper is voor de interne matrix met sleutels. De volgende code toont het gebruik van de Keys eigenschap voor geïndexeerd ophalen van sleutels uit een gesorteerde lijst met elementen met tekenreekssleutels:
string v = mySortedList.Values[3];
Dim v As String = mySortedList.Values(3)
let v = mySortedList.Values[3]
Het ophalen van de waarde van deze eigenschap is een O(1)-bewerking.