SortedList<TKey,TValue> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示基于相关的 IComparer<T> 实现按键进行排序的键/值对的集合。
generic <typename TKey, typename TValue>
public ref class SortedList : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyDictionary<TKey, TValue>, System::Collections::IDictionary
generic <typename TKey, typename TValue>
public ref class SortedList : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::IDictionary
public class SortedList<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SortedList<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SortedList<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
type SortedList<'Key, 'Value> = class
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection
interface IDictionary
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SortedList<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IDictionary
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SortedList<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary
interface ICollection
interface IReadOnlyDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SortedList<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface IDictionary
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
Public Class SortedList(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue)), IReadOnlyDictionary(Of TKey, TValue)
Public Class SortedList(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue))
类型参数
- TKey
集合中的键的类型。
- TValue
集合中值的类型。
- 继承
-
SortedList<TKey,TValue>
- 派生
- 属性
- 实现
-
ICollection<KeyValuePair<TKey,TValue>> IDictionary<TKey,TValue> IEnumerable<KeyValuePair<TKey,TValue>> IEnumerable<T> IReadOnlyCollection<KeyValuePair<TKey,TValue>> IReadOnlyDictionary<TKey,TValue> ICollection IDictionary IEnumerable
示例
下面的代码示例使用字符串键创建一个空 SortedList<TKey,TValue> 字符串,并使用 Add 方法添加一些元素。 该示例演示方法在 Add 尝试添加重复键时引发 ArgumentException 。
该示例使用 Item[] C#) 中索引器 (属性来检索值,演示 KeyNotFoundException 在请求的键不存在时引发 ,并显示可以替换与键关联的值。
该示例演示如何使用TryGetValue方法作为更高效的方法来检索值,如果程序必须经常尝试在已排序的列表中,不并显示如何使用ContainsKey方法来测试某个键是否存在之前调用Add方法。
该示例演示如何枚举排序列表中的键和值,以及如何使用 Keys 属性和 属性单独枚举键和 Values 值。
最后,该示例演示 了 Remove 方法。
#using <System.dll>
using namespace System;
using namespace System::Collections::Generic;
public ref class Example
{
public:
static void Main()
{
// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
gcnew SortedList<String^, String^>();
// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is
// already in the list.
try
{
openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console::WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console::WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";
// The indexer throws an exception if the requested key is
// not in the list.
try
{
Console::WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
}
catch (KeyNotFoundException^)
{
Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
Console::WriteLine("Key = \"tif\" is not found.");
}
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith->ContainsKey("ht"))
{
openWith->Add("ht", "hypertrm.exe");
Console::WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
// 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);
}
// To get the values alone, use the Values property.
IList<String^>^ ilistValues = openWith->Values;
// The elements of the list are strongly typed with the
// type that was specified for the SortedList values.
Console::WriteLine();
for each( String^ s in ilistValues )
{
Console::WriteLine("Value = {0}", s);
}
// The Values property is an efficient way to retrieve
// values by index.
Console::WriteLine("\nIndexed retrieval using the Values " +
"property: Values[2] = {0}", openWith->Values[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();
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]);
// Use the Remove method to remove a key/value pair.
Console::WriteLine("\nRemove(\"doc\")");
openWith->Remove("doc");
if (!openWith->ContainsKey("doc"))
{
Console::WriteLine("Key \"doc\" is not found.");
}
}
};
int main()
{
Example::Main();
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe
Key = rtf, Value = winword.exe
Key = txt, Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = hypertrm.exe
Value = winword.exe
Value = notepad.exe
Indexed retrieval using the Values property: Values[2] = winword.exe
Key = bmp
Key = dib
Key = doc
Key = ht
Key = rtf
Key = txt
Indexed retrieval using the Keys property: Keys[2] = doc
Remove("doc")
Key "doc" is not found.
*/
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
new SortedList<string, string>();
// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is
// already in the list.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";
// The indexer throws an exception if the requested key is
// not in the list.
try
{
Console.WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
// 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);
}
// To get the values alone, use the Values property.
IList<string> ilistValues = openWith.Values;
// The elements of the list are strongly typed with the
// type that was specified for the SortedList values.
Console.WriteLine();
foreach( string s in ilistValues )
{
Console.WriteLine("Value = {0}", s);
}
// The Values property is an efficient way to retrieve
// values by index.
Console.WriteLine("\nIndexed retrieval using the Values " +
"property: Values[2] = {0}", openWith.Values[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]);
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
}
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe
Key = rtf, Value = winword.exe
Key = txt, Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = hypertrm.exe
Value = winword.exe
Value = notepad.exe
Indexed retrieval using the Values property: Values[2] = winword.exe
Key = bmp
Key = dib
Key = doc
Key = ht
Key = rtf
Key = txt
Indexed retrieval using the Keys property: Keys[2] = doc
Remove("doc")
Key "doc" is not found.
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string
' keys.
Dim openWith As New SortedList(Of String, String)
' Add some elements to the list. There are no
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' The Add method throws an exception if the new key is
' already in the list.
Try
openWith.Add("txt", "winword.exe")
Catch
Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
' The Item property is the default property, so you
' can omit its name when accessing elements.
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' The default Item property can be used to change the value
' associated with a key.
openWith("rtf") = "winword.exe"
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' If a key does not exist, setting the default Item property
' for that key adds a new key/value pair.
openWith("doc") = "winword.exe"
' The default Item property throws an exception if the requested
' key is not in the list.
Try
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
Catch
Console.WriteLine("Key = ""tif"" is not found.")
End Try
' When a program often has to try keys that turn out not to
' be in the list, TryGetValue can be a more efficient
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
Console.WriteLine("Key = ""tif"" is not found.")
End If
' ContainsKey can be used to test keys before inserting
' them.
If Not openWith.ContainsKey("ht") Then
openWith.Add("ht", "hypertrm.exe")
Console.WriteLine("Value added for key = ""ht"": {0}", _
openWith("ht"))
End If
' 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
' To get the values alone, use the Values property.
Dim ilistValues As IList(Of String) = openWith.Values
' The elements of the list are strongly typed with the
' type that was specified for the SortedList values.
Console.WriteLine()
For Each s As String In ilistValues
Console.WriteLine("Value = {0}", s)
Next s
' The Values property is an efficient way to retrieve
' values by index.
Console.WriteLine(vbLf & "Indexed retrieval using the " & _
"Values property: Values(2) = {0}", openWith.Values(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))
' Use the Remove method to remove a key/value pair.
Console.WriteLine(vbLf + "Remove(""doc"")")
openWith.Remove("doc")
If Not openWith.ContainsKey("doc") Then
Console.WriteLine("Key ""doc"" is not found.")
End If
End Sub
End Class
' This code example produces the following output:
'
'An element with Key = "txt" already exists.
'For key = "rtf", value = wordpad.exe.
'For key = "rtf", value = winword.exe.
'Key = "tif" is not found.
'Key = "tif" is not found.
'Value added for key = "ht": hypertrm.exe
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = doc, Value = winword.exe
'Key = ht, Value = hypertrm.exe
'Key = rtf, Value = winword.exe
'Key = txt, Value = notepad.exe
'
'Value = paint.exe
'Value = paint.exe
'Value = winword.exe
'Value = hypertrm.exe
'Value = winword.exe
'Value = notepad.exe
'
'Indexed retrieval using the Values property: Values(2) = winword.exe
'
'Key = bmp
'Key = dib
'Key = doc
'Key = ht
'Key = rtf
'Key = txt
'
'Indexed retrieval using the Keys property: Keys(2) = doc
'
'Remove("doc")
'Key "doc" is not found.
'
注解
泛 SortedList<TKey,TValue> 型类是具有 O (log n
) 检索的键/值对数组,其中 n 是字典中的元素数。 在此中,它类似于 SortedDictionary<TKey,TValue> 泛型类。 这两个类具有相似的对象模型,并且都具有 O (日志 n
) 检索。 这两个类的不同之处在于内存使用情况和插入和删除速度:
SortedList<TKey,TValue> 使用的内存少于 SortedDictionary<TKey,TValue>。
SortedDictionary<TKey,TValue>对未排序的数据执行更快的插入和删除操作,O (日志
n
) ,而不是 O SortedList<TKey,TValue> (n
) 。如果一次性从已排序的数据填充列表, SortedList<TKey,TValue> 比 SortedDictionary<TKey,TValue>快。
和 SortedList<TKey,TValue> 类之间的SortedDictionary<TKey,TValue>另一个区别是, SortedList<TKey,TValue> 支持通过 和 Values 属性返回Keys的集合对键和值的高效索引检索。 访问属性时无需重新生成列表,因为这些列表只是键和值的内部数组的包装器。 以下代码演示如何使用 Values 属性对字符串排序列表中的值进行索引检索:
String^ v = mySortedList->Values[3];
string v = mySortedList.Values[3];
Dim v As String = mySortedList.Values(3)
SortedList<TKey,TValue> 实现为键/值对数组,按键排序。 每个元素都可以作为 KeyValuePair<TKey,TValue> 对象进行检索。
键对象必须是不可变的,只要它们用作 中的键。SortedList<TKey,TValue> 中的每个键都必须是唯一的 SortedList<TKey,TValue> 。 键不能为 null
,但如果列表中的值类型 是引用类型, TValue
则值可以是 。
SortedList<TKey,TValue> 需要比较器实现来排序和执行比较。 默认比较器 Comparer<T>.Default 检查键类型 TKey
是否实现 System.IComparable<T> 并使用该实现(如果可用)。 否则, Comparer<T>.Default 检查键类型 TKey
是否实现 System.IComparable。 如果键类型 TKey
未实现任一 System.Collections.Generic.IComparer<T> 接口,则可以在接受 参数的构造函数重载中指定实现 comparer
。
的 SortedList<TKey,TValue> 容量是 可以容纳的元素 SortedList<TKey,TValue> 数。 将元素添加到 时 SortedList<TKey,TValue>,通过重新分配内部数组,容量会根据需要自动增加。 可以通过调用 TrimExcess 或 显式设置 属性来 Capacity 减小容量。 减少容量会重新分配内存并复制 中的所有 SortedList<TKey,TValue>元素。
仅.NET Framework:对于非常大SortedList<TKey,TValue>的对象,可以通过在运行时环境中将配置元素的 <gcAllowVeryLargeObjects>
属性设置为 enabled
,将 64 位系统上的最大容量增加到 true
20 亿个元素。
foreach
Visual Basic) C++ For Each
中 C# 语言 (for each
语句返回集合中元素的类型的对象。 由于 的 SortedList<TKey,TValue> 元素是键/值对,因此元素类型不是键的类型或值的类型。 相反,元素类型为 KeyValuePair<TKey,TValue>。 例如:
for each( KeyValuePair<int, String^> kvp in mySortedList )
{
Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<int, string> kvp in mySortedList )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of Integer, String) In mySortedList
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp
语句 foreach
是枚举器的包装器,它只允许读取集合,而不允许写入集合。
构造函数
SortedList<TKey,TValue>() |
初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 IComparer<T>。 |
SortedList<TKey,TValue>(IComparer<TKey>) |
初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有默认的初始容量并使用指定的 IComparer<T>。 |
SortedList<TKey,TValue>(IDictionary<TKey,TValue>) |
初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用默认的 IComparer<T>。 |
SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用指定的 IComparer<T>。 |
SortedList<TKey,TValue>(Int32) |
初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 IComparer<T>。 |
SortedList<TKey,TValue>(Int32, IComparer<TKey>) |
初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有指定的初始容量并使用指定的 IComparer<T>。 |
属性
Capacity |
获取或设置 SortedList<TKey,TValue> 可包含的元素数。 |
Comparer |
获取该排序列表的 IComparer<T>。 |
Count |
获取包含在 SortedList<TKey,TValue> 中的键/值对的数目。 |
Item[TKey] |
获取或设置与指定的键关联的值。 |
Keys |
获取一个按排序顺序包含 SortedList<TKey,TValue> 中的键的集合。 |
Values |
获得一个包含 SortedList<TKey,TValue> 中的值的集合。 |
方法
Add(TKey, TValue) |
将带有指定键和值的元素添加到 SortedList<TKey,TValue> 中。 |
Clear() |
从 SortedList<TKey,TValue> 中移除所有元素。 |
ContainsKey(TKey) |
确定 SortedList<TKey,TValue> 是否包含特定键。 |
ContainsValue(TValue) |
确定 SortedList<TKey,TValue> 是否包含特定值。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetEnumerator() |
返回循环访问 SortedList<TKey,TValue> 的枚举数。 |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetKeyAtIndex(Int32) |
获取与指定索引对应的键。 |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
GetValueAtIndex(Int32) |
获取与指定索引对应的值。 |
IndexOfKey(TKey) |
在整个 SortedList<TKey,TValue> 中搜索指定键并返回从零开始的索引。 |
IndexOfValue(TValue) |
在整个 SortedList<TKey,TValue> 中搜索指定的值,并返回第一个匹配项的从零开始的索引。 |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
Remove(TKey) |
从 SortedList<TKey,TValue> 中移除包含指定键的元素。 |
RemoveAt(Int32) |
移除 SortedList<TKey,TValue> 的指定索引处的元素。 |
SetValueAtIndex(Int32, TValue) |
汇报与指定索引对应的值。 |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
TrimExcess() |
如果元素数小于当前容量的 90%,将容量设置为 SortedList<TKey,TValue> 中的实际元素数。 |
TryGetValue(TKey, TValue) |
获取与指定键关联的值。 |
显式接口实现
扩展方法
适用于
线程安全性
Visual Basic 中的公共静态 (Shared
) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
SortedList<TKey,TValue>只要集合未修改,就可以同时支持多个读取器。 即便如此,通过集合枚举本质上也不是线程安全的过程。 若要确保枚举过程中的线程安全性,可以在整个枚举过程中锁定集合。 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。