SortedList<TKey,TValue>.Keys 属性

定义

获取一个按排序顺序包含 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.

适用于

另请参阅