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; }
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이(가) null인 경우

속성이 설정되어 있고 IDictionary 개체가 읽기 전용인 경우

또는

속성이 설정되어 있고 key가 컬렉션에 없으며 IDictionary의 크기가 고정된 경우

예제

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

public:
    virtual property Object^ default[Object^]
    {
        Object^ get(Object^ key)
        {
            // If this key is in the dictionary, return its value.
            int index;
            if (TryGetIndexOfKey(key, &index))
            {
                // The key was found; return its value.
                return items[index]->Value;
            }
            else
            {
                // The key was not found; return null.
                return nullptr;
            }
        }

        void set(Object^ key, Object^ value)
        {
            // If this key is in the dictionary, change its value.
            int index;
            if (TryGetIndexOfKey(key, &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 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 기존 요소를 수정하지 않습니다.

적용 대상

추가 정보