SortedDictionary<TKey,TValue>.TryGetValue(TKey, TValue) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 키와 연결된 값을 가져옵니다.
public:
virtual bool TryGetValue(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool TryGetValue(TKey key, out TValue value);
abstract member TryGetValue : 'Key * 'Value -> bool
override this.TryGetValue : 'Key * 'Value -> bool
Public Function TryGetValue (key As TKey, ByRef value As TValue) As Boolean
매개 변수
- key
- TKey
가져올 값의 키입니다.
- value
- TValue
이 메서드가 반환될 때 키가 있으면 지정된 키와 연결된 값입니다. 그렇지 않으면 매개 변수 형식의 기본값입니다 value .
반품
true
SortedDictionary<TKey,TValue> 지정된 키를 가진 요소가 포함되어 있으면 이고, false그렇지 않으면 .
구현
예외
key은 null입니다.
예제
이 예제에서는 사전에 없는 키를 자주 시도하는 프로그램에서 값을 검색하는 보다 효율적인 방법으로 메서드를 사용하는 TryGetValue 방법을 보여줍니다. 반면, 이 예제에서는 존재하지 않는 키를 검색하려고 할 때 속성(C#의 인덱서)이 예외를 throw하는 방법 Item[] 도 보여 줍니다.
이 코드 예제는 클래스에 제공된 더 큰 예제의 SortedDictionary<TKey,TValue> 일부입니다.
// When a program often has to try keys that turn out not to
// be in the dictionary, 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 dictionary, 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
// The indexer throws an exception if the requested key is
// not in the dictionary.
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 dictionary.
Try
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
Catch
Console.WriteLine("Key = ""tif"" is not found.")
End Try
설명
이 메서드는 메서드의 기능 ContainsKey 과 속성을 결합합니다 Item[] .
키를 찾을 value 수 없는 경우 매개 변수는 값 형식에 대한 적절한 기본값(예: 정수 형식 TValue, 부울 형식 및 falsenull 참조 형식의 경우 0)을 가져옵니다.
이 메서드는 O(로그 n) 작업입니다.