Dictionary<TKey,TValue>.IDictionary.Item[Object] 속성

정의

지정한 키가 있는 값을 가져오거나 설정합니다.

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

가져올 값의 키입니다.

속성 값

지정한 키와 연결된 값입니다. key가 사전에 없거나 key의 형식을 Dictionary<TKey,TValue>TKey 키 형식에 할당할 수 없으면 null입니다.

구현

예외

key이(가) null인 경우

값이 할당되어 있고 key의 형식을 Dictionary<TKey,TValue>TKey 키 형식에 할당할 수 없는 경우

또는

값이 할당되어 있고 value의 형식을 Dictionary<TKey,TValue>TValue 값 형식에 할당할 수 없는 경우

예제

다음 코드 예제에서는 사용 IDictionary.Item[] 하는 방법을 보여 줍니다는 속성 (C#의 인덱서) 인터페이스를 System.Collections.IDictionary 사용 Dictionary<TKey,TValue>하 여는 , 및 속성과 다른 Dictionary<TKey,TValue>.Item[] 방법 속성입니다.

이 예제에서는 속성과 마찬가지로 Dictionary<TKey,TValue>.Item[] 속성 Dictionary<TKey,TValue>.IDictionary.Item[] 이 기존 키와 연결된 값을 변경할 수 있으며 지정된 키가 사전에 없는 경우 새 키/값 쌍을 추가하는 데 사용할 수 있음을 보여줍니다. 또한 이 예제에서는 속성과 Dictionary<TKey,TValue>.Item[] 달리 가 사전에 없으면 key 속성 Dictionary<TKey,TValue>.IDictionary.Item[] 이 예외를 throw하지 않고 대신 null 참조를 반환하는 것을 보여줍니다. 마지막으로, 이 올바른 데이터 형식이 아니면 속성을 가져오 Dictionary<TKey,TValue>.IDictionary.Item[]key null 참조가 반환되고, 가 올바른 데이터 형식이 아닌 경우 속성을 설정하면 key 예외가 throw된다는 것을 보여 줍니다.

코드 예제는 메서드에 제공된 출력을 포함하여 더 큰 예제의 IDictionary.Add 일부입니다.

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");
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 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 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 = {0}.",
    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.");
}
' 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"]);
' 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] 사용하여 컬렉션의 특정 값에 액세스할 수 있는 기능을 제공합니다(myCollection(key) Visual Basic의 경우).

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

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

이 속성의 값을 가져오거나 설정하면 O(1) 작업에 접근합니다.

적용 대상

추가 정보