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; }
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가 사전에 없거나 key의 형식을 SortedList<TKey,TValue>TKey 키 형식에 할당할 수 없으면 null입니다.

구현

예외

key이(가) null인 경우

값이 할당되어 있고 key의 형식을 SortedList<TKey,TValue>TKey 키 형식에 할당할 수 없는 경우

또는

값이 할당되어 있고 value의 형식을 SortedList<TKey,TValue>TValue 값 형식에 할당할 수 없는 경우

예제

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

이 예제에서는 속성과 마찬가지로 SortedList<TKey,TValue>.Item[] 속성 SortedList<TKey,TValue>.IDictionary.Item[] 이 기존 키와 연결된 값을 변경할 수 있으며 지정된 키가 정렬된 목록에 없는 경우 새 키/값 쌍을 추가하는 데 사용할 수 있음을 보여줍니다. 이 예제에서는 속성과 SortedList<TKey,TValue>.Item[] 달리 가 정렬된 목록에 없으면 key 속성 SortedList<TKey,TValue>.IDictionary.Item[] 이 예외를 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

설명

이 속성은 의 키 TKey 형식에 할당할 수 없는 형식인 경우 key 를 반환 null 합니다SortedList<TKey,TValue>.

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

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

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

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

적용 대상

추가 정보