共用方式為


SortedList<TKey,TValue>.Item[TKey] 屬性

定義

取得或設定與指定之索引鍵相關聯的值。

public:
 property TValue default[TKey] { TValue get(TKey key); void set(TKey key, TValue value); };
public TValue this[TKey key] { get; set; }
member this.Item('Key) : 'Value with get, set
Default Public Property Item(key As TKey) As TValue

參數

key
TKey

索引鍵,要讀取或設定其值。

屬性值

TValue

與指定之索引鍵關聯的值。 如果找不到指定的索引鍵,則取得作業會擲回 KeyNotFoundException,且設定作業會使用指定的索引鍵建立新項目。

實作

例外狀況

keynull

會擷取該屬性,而且 key 不存在於集合中。

範例

下列程式代碼範例會 Item[] 使用屬性 (C# ) 中的索引器來擷取值,示範 KeyNotFoundException 當要求的索引鍵不存在時擲回 ,並顯示可以取代與索引鍵相關聯的值。

如果程式通常必須嘗試不在排序列表中的索引鍵值,此範例也會示範如何使用 TryGetValue 方法作為更有效率的方式來擷取值。

此程式代碼範例是針對 類別提供的較大範例的 SortedList<TKey,TValue> 一部分。

// 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 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 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 Item property is another name for the indexer, so you
// can omit its name when accessing elements.
printfn $"""For key = "rtf", value = {openWith["rtf"]}."""

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] <- "winword.exe"
printfn $"""For key = "rtf", value = {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.");
}
// 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.");
}
' 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
// The indexer throws an exception if the requested key is
// not in the list.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}."""
with 
    | :? KeyNotFoundException ->
        printfn "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.");
}
// 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.");
}
' 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
// 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.
match openWith.TryGetValue("tif") with
| true, value ->
    printfn "For key = \"tif\", value = {value}."
| false, _ ->
    printfn "Key = \"tif\" is not found."

備註

這個屬性可透過下列語法存取集合中的特定元素:myCollection[key]

索引鍵不能是 null,但如果清單中的值類型為參考型別, TValue則可以是 值。

如果在擷取值時找不到索引鍵, KeyNotFoundException 則會擲回 。 如果在設定值時找不到索引鍵,則會新增索引鍵和值。

您也可以使用 Item[] 屬性來新增專案,方法是設定不存在於 中的SortedList<TKey,TValue>索引鍵值,例如 。 myCollection["myNonexistentKey"] = myValue 不過,如果指定的索引鍵已存在於 中 SortedList<TKey,TValue>,則設定 Item[] 屬性會覆寫舊的值。 相反地, Add 方法不會修改現有的專案。

C# 語言使用 this 關鍵字定義索引,而不必實作 Item[] 屬性。 Visual Basic 會將 Item[] 實作為預設屬性,這樣會提供相同的索引功能。

擷取此屬性的值是 O (記錄 n) 作業,其中 n 是 Count。 如果索引鍵已在 中SortedList<TKey,TValue>,則設定 屬性是 O (記錄 n) 作業。 如果索引鍵不在清單中,則設定 屬性為未排序數據的 O (n) 作業,如果新元素加入清單結尾,則為 O (記錄 n) 。 如果插入會導致重設大小,作業為 O (n) 。

適用於

另請參閱