SortedList<TKey,TValue>.Item[TKey] 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 키와 연결된 값을 가져오거나 설정합니다.
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
값을 얻거나 설정할 키입니다.
속성 값
지정된 키와 연결된 값입니다. 지정된 키를 찾을 수 없으면 get 작업이 throw KeyNotFoundException 되고 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 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 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", 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의 값 형식이 참조 형식인 경우일 수 있습니다.
값을 검색 KeyNotFoundException 할 때 키를 찾을 수 없으면 throw됩니다. 값을 설정할 때 키를 찾을 수 없으면 키와 값이 추가됩니다.
에 없는 SortedList<TKey,TValue>키의 값을 설정 하 여 새 요소를 추가 하는 속성을 사용할 Item[] 수도 있습니다. 예를 들어. myCollection["myNonexistentKey"] = myValue 그러나 지정된 키가 이미 있는 SortedList<TKey,TValue>경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.
C# 언어는 키워드를 this 사용하여 속성을 구현하는 대신 인덱서를 정의합니다 Item[] . Visual Basic은 Item[] 동일한 인덱싱 기능을 제공하는 기본 속성으로 구현합니다.
이 속성의 값을 검색하는 것은 O(로그 n) 작업이며 여기서 n은 .입니다 Count. 키가 이미 SortedList<TKey,TValue>있는 경우 속성 설정은 O(로그n) 작업입니다. 키가 목록에 없으면 속성 설정은 정렬되지 않은 데이터에 대한 O(n) 작업이거나, 목록 끝에 새 요소가 추가되면 O(log n)입니다. 삽입으로 인해 크기가 조정되는 경우 작업은 O(n)입니다.