SortedList<TKey,TValue>.Keys 属性

定义

获取一个按排序顺序包含 SortedList<TKey,TValue> 中的键的集合。

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> 中的键。

示例

下面的代码示例演示如何使用 Keys 属性枚举排序列表中的键,以及如何枚举排序列表中的键和值。

该示例还演示如何使用 Keys 属性高效检索键的索引。

此代码是可以编译和执行的较大示例的一部分。 请参阅 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}"

注解

IList<T> 键的顺序与 中 SortedList<TKey,TValue>的顺序相同。

返回的 IList<T> 不是静态副本;相反, IList<T> 引用回原始 SortedList<TKey,TValue>中的键。 因此,对 SortedList<TKey,TValue> 的更改将继续反映在 中 IList<T>

属性 Keys 返回的集合提供了一种按索引检索键的有效方法。 访问 属性时,无需重新生成列表,因为该列表只是键的内部数组的包装器。 以下代码演示如何使用 Keys 属性从具有字符串键的已排序元素列表中检索键的索引:

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

检索此属性的值的运算复杂度为 O(1)。

适用于

另请参阅