SortedList<TKey,TValue>.Item[TKey] 속성

정의

지정된 키에 연결된 값을 가져오거나 설정합니다.

public:
 property TValue default[TKey] { TValue get(TKey key); void set(TKey key, TValue value); };
public TValue this[TKey key] { get; set; }
member this.Item('Key) : 'Value with get, set
Default Public Property Item(key As TKey) As TValue

매개 변수

key
TKey

가져오거나 설정할 값이 있는 키입니다.

속성 값

TValue

지정한 키와 연결된 값입니다. 지정한 키가 없으면 get 작업에서 KeyNotFoundException을 throw하고 set 작업에서 지정한 키를 사용하여 새 요소를 만듭니다.

구현

예외

key이(가) null인 경우

속성을 검색할 때 컬렉션에 key가 없는 경우

예제

다음 코드 예제에서는 속성(C#의 인덱서)을 사용하여 Item[] 값을 검색하고 요청된 키가 없을 때 이 throw됨을 KeyNotFoundException 보여 줍니다. 키와 연결된 값을 바꿀 수 있음을 보여 줍니다.

또한 프로그램에서 정렬된 목록에 없는 키 값을 시도해야 하는 경우 메서드를 보다 효율적인 방법으로 사용하여 TryGetValue 값을 검색하는 방법도 보여 집니다.

이 코드 예제는에 대해 제공 된 큰 예제의 일부는 SortedList<TKey,TValue> 클래스입니다.

// 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 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 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 Item property is another name for the indexer, so you
// can omit its name when accessing elements.
printfn $"""For key = "rtf", value = {openWith["rtf"]}."""

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] <- "winword.exe"
printfn $"""For key = "rtf", value = {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 throws an exception if the requested key is
// not in the list.
try
{
    Console::WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException^)
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console.WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException)
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// The indexer throws an exception if the requested key is
// not in the list.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}."""
with 
    | :? KeyNotFoundException ->
        printfn "Key = \"tif\" is not found."
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' When a program often has to try keys that turn out not to
' be in the list, TryGetValue can be a more efficient 
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
    Console.WriteLine("Key = ""tif"" is not found.")
End If
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
match openWith.TryGetValue("tif") with
| true, value ->
    printfn "For key = \"tif\", value = {value}."
| false, _ ->
    printfn "Key = \"tif\" is not found."

설명

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

키는 일 수 없지만 null목록 TValue의 값 형식이 참조 형식인 경우 값이 될 수 있습니다.

값을 검색할 때 키를 찾을 수 없으면 이 throw됩니다 KeyNotFoundException . 값을 설정할 때 키를 찾을 수 없으면 키와 값이 추가됩니다.

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

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

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

적용 대상

추가 정보