Dictionary<TKey,TValue>.IDictionary.Contains(Object) 메서드

정의

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

 virtual bool System.Collections.IDictionary.Contains(System::Object ^ key) = System::Collections::IDictionary::Contains;
bool IDictionary.Contains (object key);
abstract member System.Collections.IDictionary.Contains : obj -> bool
override this.System.Collections.IDictionary.Contains : obj -> bool
Function Contains (key As Object) As Boolean Implements IDictionary.Contains

매개 변수

key
Object

IDictionary에서 찾을 수 있는 키입니다.

반환

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

구현

예외

key이(가) null인 경우

예제

다음 코드 예제를 사용 하는 방법을 보여 주는 메서드를 System.Collections.IDictionary 사용 하 여 IDictionary.Contains 인터페이스는 합니다Dictionary<TKey,TValue>. 이 예제에서는 잘못된 데이터 형식의 키가 제공되면 메서드가 를 반환 false 하는 것을 보여 줍니다.

코드 예제는 메서드에 제공된 출력을 포함하여 더 큰 예제의 IDictionary.Add 일부입니다.

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new Dictionary<string, string>();

        // Add some elements to the dictionary. There are no
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
// Contains can be used to test keys before inserting
// them.
if (!openWith.Contains("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}",
        openWith["ht"]);
}

// IDictionary.Contains returns false if the wrong data
// type is supplied.
Console.WriteLine("openWith.Contains(29.7) returns {0}",
    openWith.Contains(29.7));
' Contains can be used to test keys before inserting 
' them.
If Not openWith.Contains("ht") Then
    openWith.Add("ht", "hypertrm.exe")
    Console.WriteLine("Value added for key = ""ht"": {0}", _
        openWith("ht"))
End If

' IDictionary.Contains returns False if the wrong data 
' type is supplied.
Console.WriteLine("openWith.Contains(29.7) returns {0}", _
    openWith.Contains(29.7))
    }
}

    End Sub

End Class

설명

이 메서드는 의 키 TKey 형식에 할당할 수 없는 형식인 경우 key 를 반환 false 합니다Dictionary<TKey,TValue>.

이 메서드는 O(1) 작업에 접근합니다.

적용 대상