IDictionary<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

지정한 키가 있는 요소입니다.

예외

key이(가) null인 경우

속성이 검색되었지만 key가 없는 경우

속성이 설정되어 있으며 IDictionary<TKey,TValue>가 읽기 전용인 경우

예제

다음 코드 예제에서는 속성(C#의 인덱서)을 사용하여 Item[] 값을 검색하고 요청된 키가 없을 때 이 throw됨을 KeyNotFoundException 보여 줍니다. 키와 연결된 값을 바꿀 수 있음을 보여 줍니다.

이 예제에서는 프로그램에서 사전에 없는 키 값을 자주 시도해야 하는 경우 메서드를 보다 효율적인 방법으로 사용하여 TryGetValue 값을 검색하는 방법을 보여줍니다.

이 코드는 컴파일 및 실행할 수 있는 더 큰 예제의 일부입니다. System.Collections.Generic.IDictionary<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 indexer throws an exception if the requested key is
// not in the dictionary.
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 dictionary.
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 dictionary.
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 dictionary, 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 dictionary, 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 dictionary, 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

설명

이 속성은 (Visual Basic의 경우)myCollection(key) 구문을 myCollection[key] 사용하여 컬렉션의 특정 요소에 액세스할 수 있는 기능을 제공합니다.

사전에 없는 키의 값을 설정하여 속성을 사용하여 Item[] 새 요소를 추가할 수도 있습니다(예 myCollection["myNonexistentKey"] = myValue : C#(myCollection("myNonexistentKey") = myValue Visual Basic의 경우). 그러나 지정된 키가 사전에 이미 있는 경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.

구현은 개체의 같음을 결정하는 방법에 따라 달라질 수 있습니다. 예를 들어 클래스는 List<T> 를 사용하는 Comparer<T>.Default반면 클래스는 Dictionary<TKey,TValue> 사용자가 키를 비교하는 데 사용할 구현을 지정할 IComparer<T> 수 있도록 합니다.

C# 언어는 키워드(keyword) 사용하여 속성을 구현하는 대신 인덱서를 정의합니다Item[]. Visual Basic에서는 동일한 인덱싱 기능을 제공하는 Item[]을 기본 속성으로 구현합니다.

구현은 가 되도록 허용할 key 지 여부에 따라 달라질 수 있습니다 null.

적용 대상

추가 정보