PropertyCollection.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; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
 Property Item(key As Object) As Object Implements IDictionary.Item

매개 변수

key
Object

가져오기 또는 설정할 요소의 키입니다.

속성 값

지정된 키를 가진 요소입니다.

구현

예외

keynull입니다.

속성이 설정되고 개체가 IDictionary 읽기 전용입니다.

-또는-

속성이 설정되고 컬렉션 key 에 존재하지 않으며 크기가 IDictionary 고정되어 있습니다.

예제

다음 예제에서는 속성을 구현 Item[] 하는 방법을 보여줍니다. 이 코드 예제는 클래스에 제공된 더 큰 예제의 IDictionary 일부입니다.

public object this[object key]
{
    get
    {
        // If this key is in the dictionary, return its value.
        Int32 index;
        if (TryGetIndexOfKey(key, out index))
        {
            // The key was found; return its value.
            return items[index].Value;
        }
        else
        {
            // The key was not found; return null.
            return null;
        }
    }

    set
    {
        // If this key is in the dictionary, change its value.
        Int32 index;
        if (TryGetIndexOfKey(key, out index))
        {
            // The key was found; change its value.
            items[index].Value = value;
        }
        else
        {
            // This key is not in the dictionary; add this key/value pair.
            Add(key, value);
        }
    }
}
Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item
    Get

        ' If this key is in the dictionary, return its value.
        Dim index As Integer
        If TryGetIndexOfKey(key, index) Then

            ' The key was found return its value.
            Return items(index).Value
        Else

            ' The key was not found return null.
            Return Nothing
        End If
    End Get

    Set(ByVal value As Object)
        ' If this key is in the dictionary, change its value. 
        Dim index As Integer
        If TryGetIndexOfKey(key, index) Then

            ' The key was found change its value.
            items(index).Value = value
        Else

            ' This key is not in the dictionary add this key/value pair.
            Add(key, value)
        End If
    End Set
End Property

설명

이 속성은 다음 구문을 myCollection[key]사용하여 컬렉션의 특정 요소에 액세스하는 기능을 제공합니다.

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

적용 대상

추가 정보