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 (記錄 n
檔) 擷取,其中 n 是字典中的元素數目。 在此中,它類似于 SortedDictionary<TKey,TValue> 泛型類別。 這兩個類別具有類似的物件模型,而且兩者都有 O (記錄 n
) 擷取。 兩個類別的差異在於記憶體使用量和插入和移除的速度:
SortedList<TKey,TValue> 使用的記憶體小於 SortedDictionary<TKey,TValue> 。
SortedDictionary<TKey,TValue>對於未排序的資料,O (記錄
n
檔) 具有較快的插入和移除作業,而不是 針對 SortedList<TKey,TValue> 的 O (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> 的物件,您可以將 64 位系統上的最大容量增加到 200 億個元素,方法是在執行時間環境中將組態元素的 <gcAllowVeryLargeObjects>
屬性設定 enabled
為 true
。
foreach
C++ 中 C# 語言 (for each
的 語句, For Each
在 Visual Basic 中) 會傳回集合中專案類型的 物件。 因為 的 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> 同時支援多個讀取器。 即使如此,透過集合列舉本質上不是安全線程程式。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。
另請參閱
意見反應
提交並檢視相關的意見反應