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; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

매개 변수

key
Object

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

속성 값

지정된 키가 있는 요소이거나 null 키가 없는 경우입니다.

예외

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

구현은 키를 허용하는지 여부에 따라 달라질 수 있습니다 null.

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

적용 대상

추가 정보