SortedList<TKey,TValue>.ContainsKey(TKey) 메서드

정의

SortedList<TKey,TValue>에 특정 키가 들어 있는지 여부를 확인합니다.

public:
 virtual bool ContainsKey(TKey key);
public bool ContainsKey (TKey key);
abstract member ContainsKey : 'Key -> bool
override this.ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean

매개 변수

key
TKey

SortedList<TKey,TValue>에서 찾을 수 있는 키입니다.

반환

true에 지정한 키가 있는 요소가 포함되어 있으면 SortedList<TKey,TValue>이고, 그렇지 않으면 false입니다.

구현

예외

key이(가) null인 경우

예제

다음 코드 예제를 사용 하는 방법을 보여 줍니다.는 ContainsKey 메서드를 호출 Add 하기 전에 키가 있는지 여부를 테스트 하는 방법입니다. 또한 프로그램에서 정렬된 목록에 없는 키를 자주 시도할 때 값을 검색하는 효율적인 방법인 값을 검색하는 메서드를 사용하는 TryGetValue 방법을 보여줍니다. 마지막으로, 속성(C#의 인덱서)을 사용하여 Item[] 키가 존재하는지 여부를 테스트하는 가장 효율적인 방법을 보여 줍니다.

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

// ContainsKey can be used to test keys before inserting
// them.
if (!openWith->ContainsKey("ht"))
{
    openWith->Add("ht", "hypertrm.exe");
    Console::WriteLine("Value added for key = \"ht\": {0}",
        openWith["ht"]);
}
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}",
        openWith["ht"]);
}
' ContainsKey can be used to test keys before inserting 
' them.
If Not openWith.ContainsKey("ht") Then
    openWith.Add("ht", "hypertrm.exe")
    Console.WriteLine("Value added for key = ""ht"": {0}", _
        openWith("ht"))
End If
// ContainsKey can be used to test keys before inserting
// them.
if not (openWith.ContainsKey("ht")) then
    openWith.Add("ht", "hypertrm.exe");
    printfn """Value added for key = "ht": {openWith["ht"]}"""
// 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."
// 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."

설명

이 메서드는 O(로그 n) 작업이며 여기서 n 는 입니다 Count.

적용 대상

추가 정보