Bagikan melalui


SortedList<TKey,TValue>.Keys Properti

Definisi

Mendapatkan koleksi yang berisi kunci dalam SortedList<TKey,TValue>, dalam urutan yang diurutkan.

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)

Nilai Properti

IList<TKey>

yang IList<T> berisi kunci di SortedList<TKey,TValue>.

Contoh

Contoh kode berikut menunjukkan cara menghitung kunci dalam daftar yang diurutkan menggunakan Keys properti , dan cara menghitung kunci dan nilai dalam daftar yang diurutkan.

Contohnya juga menunjukkan cara menggunakan Keys properti untuk pengambilan kunci terindeks yang efisien.

Kode ini adalah bagian dari contoh yang lebih besar yang dapat dikompilasi dan dijalankan. Lihat 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();
for each( 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.
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();
for each( 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();
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}"

Keterangan

Urutan kunci dalam IList<T> sama dengan urutan di SortedList<TKey,TValue>.

yang dikembalikan IList<T> bukan salinan statis; sebaliknya, IList<T> mengacu kembali ke kunci di aslinya SortedList<TKey,TValue>. Oleh karena itu, perubahan pada SortedList<TKey,TValue> terus tercermin dalam IList<T>.

Koleksi yang Keys dikembalikan oleh properti menyediakan cara yang efisien untuk mengambil kunci menurut indeks. Tidak perlu meregenerasi daftar ketika properti diakses, karena daftar hanyalah pembungkus untuk array kunci internal. Kode berikut menunjukkan penggunaan Keys properti untuk pengambilan kunci terindeks dari daftar elemen yang diurutkan dengan kunci string:

String^ v = mySortedList->Values[3];
string v = mySortedList.Values[3];
Dim v As String = mySortedList.Values(3)
let v = mySortedList.Values[3]

Mengambil nilai properti ini adalah operasi O(1).

Berlaku untuk

Lihat juga