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

매개 변수

key
Object

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

속성 값

지정된 키가 있는 요소이거나 null 사전에 key 없거나 키 형식에 할당할 수 SortedList<TKey,TValue>TKey 없는 형식인 경우 key

구현

예외

keynull입니다.

값이 할당되고 있으며 key 키 형식에 할당할 수 없는 형식 TKeySortedList<TKey,TValue>입니다.

-또는-

값이 할당되고 있으며 값 형식에 할당할 수 없는 형식 TValue 입니다 SortedList<TKey,TValue>.

예제

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

이 예제에서는 속성 SortedList<TKey,TValue>.IDictionary.Item[] 과 마찬가지로 SortedList<TKey,TValue>.Item[] 속성이 기존 키와 연결된 값을 변경할 수 있으며 지정된 키가 정렬된 목록에 없는 경우 새 키/값 쌍을 추가하는 데 사용할 수 있음을 보여줍니다. 또한 이 예제에서는 속성 SortedList<TKey,TValue>.IDictionary.Item[]SortedList<TKey,TValue>.Item[] 달리 정렬된 목록에 없는 경우 key 속성이 예외를 throw하지 않고 null 참조를 대신 반환하는 것을 보여 있습니다. 마지막으로, 이 예제에서는 속성이 올바른 데이터 형식이 SortedList<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 sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string, string>();

        // Add some elements to the sorted list. 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 sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String, String)
        
        ' Add some elements to the sorted list. 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 SorteList class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the sorted list.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
' Unlike the default Item property on the SortedList class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the sorted list.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

설명

이 속성은 키 형식에 할당할 수 SortedList<TKey,TValue>TKey 없는 형식이면 key 반환 null 됩니다.

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

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

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

이 속성의 값을 검색하는 것은 O(로그 n) 작업이며 여기서 n은 .입니다 Count. 키가 이미 SortedList<TKey,TValue>있는 경우 속성 설정은 O(로그n) 작업입니다. 키가 목록에 없으면 속성 설정은 정렬되지 않은 데이터에 대한 O(n) 작업이거나, 목록 끝에 새 요소가 추가되면 O(log n)입니다. 삽입으로 인해 크기가 조정되는 경우 작업은 O(n)입니다.

적용 대상

추가 정보