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

정의

지정된 키를 갖는 요소가 IDictionary<TKey,TValue>에 들어 있는지 여부를 결정합니다.

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

매개 변수

key
TKey

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

반환

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

예외

key이(가) null인 경우

예제

다음 코드 예제를 사용 하는 방법을 보여 줍니다.는 ContainsKey 메서드를 호출 Add 하기 전에 키가 있는지 여부를 테스트 하는 방법입니다. 또한 프로그램이 사전에 없는 키 값을 자주 시도하는 경우 값을 검색하는 보다 효율적인 방법이 될 수 있는 메서드를 사용하는 TryGetValue 방법도 보여 집니다. 마지막으로 속성을 사용하여 Item[] 항목을 삽입하는 방법을 보여 줍니다(C#의 인덱서).

이 코드는 컴파일 및 실행할 수 있는 더 큰 예제의 일부입니다. System.Collections.Generic.IDictionary<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
// 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", 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.
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 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

설명

구현은 개체의 같음을 결정하는 방법에 따라 달라질 수 있습니다. 예를 들어 클래스는 List<T> 를 사용하는 Comparer<T>.Default반면 Dictionary<TKey,TValue> , 클래스를 사용하면 사용자가 키를 비교하는 데 사용할 구현을 지정할 IComparer<T> 수 있습니다.

구현은 가 되도록 허용할 key 지 여부에 따라 달라질 수 있습니다 null.

적용 대상