SortedList<TKey,TValue>.Keys SortedList<TKey,TValue>.Keys SortedList<TKey,TValue>.Keys SortedList<TKey,TValue>.Keys Property

定義

取得包含 SortedList<TKey,TValue> 中的索引鍵 (依已排序的順序) 的集合。Gets a collection containing the keys in the SortedList<TKey,TValue>, in sorted order.

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)

屬性值

IList<TKey>

IList<T>,包含 SortedList<TKey,TValue> 中的索引鍵。A IList<T> containing the keys in the SortedList<TKey,TValue>.

範例

下列程式碼範例示範如何使用Keys屬性列舉已排序清單中的索引鍵,以及如何列舉已排序清單中的索引鍵和值。The following code example shows how to enumerate the keys in the sorted list using the Keys property, and how to enumerate the keys and values in the sorted list.

此範例也會示範如何使用Keys屬性來有效率地編制索引鍵的索引抓取。The example also shows how to use the Keys property for efficient indexed retrieval of keys.

此程式碼是可編譯和執行之較大範例的一部分。This code is part of a larger example that can be compiled and executed. 請參閱 SortedList<TKey,TValue>See 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))
// 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

備註

IList<T>的索引鍵順序與SortedList<TKey,TValue>中的順序相同。The order of the keys in the IList<T> is the same as the order in the SortedList<TKey,TValue>.

傳回IList<T>的不是靜態複本IList<T> ,而是指回原始SortedList<TKey,TValue>中的索引鍵。The returned IList<T> is not a static copy; instead, the IList<T> refers back to the keys in the original SortedList<TKey,TValue>. 因此,的SortedList<TKey,TValue>變更會繼續反映IList<T>在中。Therefore, changes to the SortedList<TKey,TValue> continue to be reflected in the IList<T>.

Keys屬性所傳回的集合會提供有效率的方式,依索引來抓取金鑰。The collection returned by the Keys property provides an efficient way to retrieve keys by index. 當存取屬性時,不需要重新產生清單,因為清單只是索引鍵內部陣列的包裝函式。It is not necessary to regenerate the list when the property is accessed, because the list is just a wrapper for the internal array of keys. 下列程式碼示範如何使用Keys屬性,從具有字串索引鍵之專案的已排序清單中,對索引鍵進行索引抓取:The following code shows the use of the Keys property for indexed retrieval of keys from a sorted list of elements with string keys:

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

擷取這個屬性的值是一種 O(1) 運算。Retrieving the value of this property is an O(1) operation.

適用於

另請參閱