IDictionary.Item[Object] 속성

정의

지정한 키를 가진 요소를 가져오거나 설정합니다.

public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
public object this[object key] { get; set; }
public object? this[object key] { get; set; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

매개 변수

key
Object

가져오거나 설정할 요소의 키입니다.

속성 값

지정된 키를 가진 속성입니다. 해당 키가 없는 경우 null입니다.

예외

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 기존 요소를 수정하지 않습니다.

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

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

적용 대상

추가 정보