Dictionary<TKey,TValue>.IDictionary.Item[Object] プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したキーを持つ値を取得または設定します。
property System::Object ^ System::Collections::IDictionary::Item[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
object System.Collections.IDictionary.Item[object key] { get; set; }
object? System.Collections.IDictionary.Item[object key] { get; set; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
Property Item(key As Object) As Object Implements IDictionary.Item
パラメーター
- key
- Object
取得する値のキー。
プロパティ値
指定したキーに関連付けられた値。または、nullがディクショナリに含まれていないか、keyがkeyのキー型TKeyに割り当てられない型の場合にDictionary<TKey,TValue>します。
実装
例外
key は nullです。
値が割り当てられ、keyはTKeyのキー型Dictionary<TKey,TValue>に割り当てられない型です。
-又は-
値が割り当てられ、TValueの値型Dictionary<TKey,TValue>に割り当てることができない型です。
例
次のコード例は、IDictionary.Item[] インターフェイスの System.Collections.IDictionary プロパティ (C#のインデクサー) をDictionary<TKey,TValue>と共に使用する方法と、プロパティが Dictionary<TKey,TValue>.Item[] プロパティと異なる方法を示しています。
この例では、 Dictionary<TKey,TValue>.Item[] プロパティと同様に、 Dictionary<TKey,TValue>.IDictionary.Item[] プロパティは既存のキーに関連付けられている値を変更でき、指定したキーがディクショナリにない場合に新しいキーと値のペアを追加するために使用できることを示しています。 この例では、Dictionary<TKey,TValue>.Item[] プロパティとは異なり、Dictionary<TKey,TValue>.IDictionary.Item[]がディクショナリにない場合、key プロパティは例外をスローせず、代わりに null 参照を返すことも示しています。 最後に、 Dictionary<TKey,TValue>.IDictionary.Item[] プロパティを取得すると、 key が正しいデータ型でない場合は null 参照が返され、 key が正しいデータ型でない場合はプロパティを設定すると例外がスローされることを示します。
using System;
using System.Collections;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
//
IDictionary openWith = new Dictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.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 = {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 = {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 returns null if the key is of the wrong data
// type.
Console.WriteLine("The indexer returns null"
+ " if the key is of the wrong type:");
Console.WriteLine($"For key = 2, value = {openWith[2]}.");
// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
{
openWith[2] = "This does not get added.";
}
catch (ArgumentException)
{
Console.WriteLine("A key of the wrong type was specified"
+ " when assigning to the indexer.");
}
// Unlike the default Item property on the Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
Console.WriteLine($"""For key = "tif", value = {openWith["tif"]}.""");
}
}
open System
open System.Collections
open System.Collections.Generic
// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
let openWith: IDictionary = Dictionary<string, string>()
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.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 returns null if the key is of the wrong data
// type.
printfn "The indexer returns null if the key is of the wrong type:"
printfn $"""For key = 2, value = {openWith.[2]}."""
// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
openWith[2] <- "This does not get added."
with :? ArgumentException ->
printfn "A key of the wrong type was specified when assigning to the indexer."
// Unlike the default Item property on the Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
printfn $"""For key = "tif", value = {openWith.["tif"]}."""
Imports System.Collections
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new dictionary of strings, with string keys,
' and access it using the IDictionary interface.
'
Dim openWith As IDictionary = _
New Dictionary(Of String, String)
' Add some elements to the dictionary. There are no
' duplicate keys, but some of the values are duplicates.
' IDictionary.Add throws an exception if incorrect types
' are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.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 default Item property returns Nothing if the key
' is of the wrong data type.
Console.WriteLine("The default Item property returns Nothing" _
& " if the key is of the wrong type:")
Console.WriteLine("For key = 2, value = {0}.", _
openWith(2))
' The default Item property throws an exception when setting
' a value if the key is of the wrong data type.
Try
openWith(2) = "This does not get added."
Catch
Console.WriteLine("A key of the wrong type was specified" _
& " when setting the default Item property.")
End Try
' Unlike the default Item property on the Dictionary class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the dictionary.
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
End Sub
End Class
注釈
このプロパティは、次の C# 構文を使用してコレクション内の特定の値にアクセスする機能を提供します: myCollection[key] (Visual BasicのmyCollection(key))。
Item[] プロパティを使用して、ディクショナリに存在しないキーの値 (myCollection["myNonexistentKey"] = myValueなど) を設定して、新しい要素を追加することもできます。 ただし、指定したキーがディクショナリに既に存在する場合は、 Item[] プロパティを設定すると、古い値が上書きされます。 これに対し、 Add メソッドは既存の要素を変更しません。
C# 言語では、 プロパティを実装する代わりに、IDictionary.Item[]キーワードを使用してインデクサーを定義します。 Visual Basic では、 IDictionary.Item[] を既定のプロパティとして実装します。このプロパティは、同じインデックス作成機能を提供します。
このプロパティの値を取得または設定すると、O(1) 操作に近づく。