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

指定したキーに関連付けられている値。 指定したキーが見つからない場合、get 操作は KeyNotFoundException をスローし、set 操作は指定したキーを使用して新しい要素を作成します。

実装

例外

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 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 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", 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) になります。

適用対象

こちらもご覧ください