Dictionary<TKey,TValue> 생성자

정의

Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

오버로드

Dictionary<TKey,TValue>()

기본 초기 용량을 갖고 있고 키 형식에 대한 기본 같음 비교자를 사용하는 비어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 키 형식에 대한 기본 같음 비교자를 사용하는 IDictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

지정된 IEnumerable<T>에서 복사한 요소가 들어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(IEqualityComparer<TKey>)

기본 초기 용량을 갖고 있고 지정된 Dictionary<TKey,TValue>을 사용하는 비어 있는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(Int32)

지정한 초기 용량을 갖고 있고 키 형식에 대한 기본 같음 비교자를 사용하는 비어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IDictionary<TKey,TValue>을 사용하는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IEnumerable<T>을 사용하는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)

지정된 초기 용량을 갖고 있고 지정된 Dictionary<TKey,TValue>을 사용하는 비어 있는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
사용되지 않음.

serialize된 데이터를 사용하여 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

Dictionary<TKey,TValue>()

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

기본 초기 용량을 갖고 있고 키 형식에 대한 기본 같음 비교자를 사용하는 비어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary();
public Dictionary ();
Public Sub New ()

예제

다음 코드 예제에서는 문자열 키가 있는 빈 Dictionary<TKey,TValue> 문자열을 만들고 메서드를 Add 사용하여 일부 요소를 추가합니다. 이 예제에서는 메서드가 Add 중복 키를 추가하려고 할 때 을 throw ArgumentException 하는 것을 보여 줍니다.

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

// Create a new dictionary of strings, with string keys.
//
Dictionary<String^, String^>^ openWith =
    gcnew Dictionary<String^, String^>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

설명

의 모든 키는 Dictionary<TKey,TValue> 기본 같음 비교자에 따라 고유해야 합니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다. 또는 매개 변수를 허용하는 생성자를 사용하여 제네릭 인터페이스의 IEqualityComparer<T> 구현을 comparer 지정할 수 있습니다.

참고

컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하는 생성자를 사용하면 에 요소를 Dictionary<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

이 생성자는 O(1) 작업입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 키 형식에 대한 기본 같음 비교자를 사용하는 IDictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public Dictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue))

매개 변수

dictionary
IDictionary<TKey,TValue>

요소가 새 IDictionary<TKey,TValue>에 복사되는 Dictionary<TKey,TValue>입니다.

예외

dictionary이(가) null인 경우

dictionary에 중복 키가 하나 이상 포함된 경우

예제

다음 코드 예제에서는 다른 사전에서 정렬 된 콘텐츠를 사용 하 여 초기화 Dictionary<TKey,TValue> 하는 생성자를 사용 Dictionary<TKey,TValue>(IEqualityComparer<TKey>) 하는 방법을 보여 입니다. 이 코드 예제에서는 를 SortedDictionary<TKey,TValue> 만들고 임의 순서로 데이터로 채웁니다. 그런 다음 를 생성자에 전달 SortedDictionary<TKey,TValue> 하여 Dictionary<TKey,TValue>(IEqualityComparer<TKey>) 정렬된 을 Dictionary<TKey,TValue> 만듭니다. 이는 특정 시점에 정적이 되는 정렬된 사전을 빌드해야 하는 경우에 유용합니다. 에서 SortedDictionary<TKey,TValue> 로 데이터를 복사하면 Dictionary<TKey,TValue> 검색 속도가 향상됩니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted dictionary of strings, with string
        // keys.
        SortedDictionary<string, string> openWith =
            new SortedDictionary<string, string>();

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Create a Dictionary of strings with string keys, and
        // initialize it with the contents of the sorted dictionary.
        Dictionary<string, string> copy =
            new Dictionary<string, string>(openWith);

        // List the contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}",
               kvp.Key, kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted dictionary of strings, with string 
        ' keys.
        Dim openWith As New SortedDictionary(Of String, String)
        
        ' Add some elements to the sorted dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a Dictionary of strings with string keys, and 
        ' initialize it with the contents of the sorted dictionary.
        Dim copy As New Dictionary(Of String, String)(openWith)

        ' List the contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

Dictionary<TKey,TValue> 모든 키는 기본 같음 비교자에 따라 고유해야 합니다. 마찬가지로 원본 dictionary 의 모든 키도 기본 같음 비교자에 따라 고유해야 합니다.

Dictionary<TKey,TValue> 의 초기 용량은 의 모든 요소를 dictionary포함할 수 있을 만큼 큽습니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다. 또는 매개 변수를 허용하는 생성자를 사용하여 제네릭 인터페이스의 IEqualityComparer<T> 구현을 comparer 지정할 수 있습니다.

이 생성자는 O(n) 작업입니다. 여기서 n은 의 dictionary요소 수입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정된 IEnumerable<T>에서 복사한 요소가 들어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ collection);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection);
new System.Collections.Generic.Dictionary<'Key, 'Value> : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (collection As IEnumerable(Of KeyValuePair(Of TKey, TValue)))

매개 변수

collection
IEnumerable<KeyValuePair<TKey,TValue>>

요소가 새 IEnumerable<T>에 복사되는 Dictionary<TKey,TValue>입니다.

예외

collection이(가) null인 경우

collection에 중복 키가 하나 이상 포함된 경우

적용 대상

Dictionary<TKey,TValue>(IEqualityComparer<TKey>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

기본 초기 용량을 갖고 있고 지정된 Dictionary<TKey,TValue>을 사용하는 비어 있는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (System.Collections.Generic.IEqualityComparer<TKey> comparer);
public Dictionary (System.Collections.Generic.IEqualityComparer<TKey>? comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (comparer As IEqualityComparer(Of TKey))

매개 변수

comparer
IEqualityComparer<TKey>

키를 비교할 때 사용할 IEqualityComparer<T> 구현을 지정하거나, 해당 키 형식에 기본 EqualityComparer<T>을 사용하려면 null을 지정합니다.

예제

다음 코드 예제에서는 현재 문화권에 대/소문자를 구분하지 않는 같음 비교자를 사용하여 을 만듭니다 Dictionary<TKey,TValue> . 이 예제에서는 소문자 키가 있는 요소와 대문자 키가 있는 요소 4개를 추가합니다. 그런 다음, 사례별로만 기존 키와 다른 키가 있는 요소를 추가하고 결과 예외를 catch하고 오류 메시지를 표시하는 예제입니다. 마지막으로, 이 예제에서는 사전에 요소를 표시합니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys
        // and a case-insensitive comparer for the current culture.
        Dictionary<string, string> openWith =
                      new Dictionary<string, string>(
                          StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same
        // except for case; this would be allowed with the default
        // comparer.
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the dictionary.");
        }

        // List the contents of the sorted dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

BMP is already in the dictionary.

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New Dictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' Try to add a fifth element with a key that is the same 
        ' except for case; this would be allowed with the default
        ' comparer.
        Try
            openWith.Add("BMP", "paint.exe")
        Catch ex As ArgumentException
            Console.WriteLine(vbLf & "BMP is already in the dictionary.")
        End Try
        
        ' List the contents of the dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'BMP is already in the dictionary.
'
'Key = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe

설명

대/소문자를 구분하지 않는 문자열 키를 사용하여 사전을 만들려면 클래스에서 제공하는 StringComparer 대/소문자를 구분하지 않는 문자열 비교자와 함께 이 생성자를 사용합니다.

의 모든 키는 Dictionary<TKey,TValue> 지정된 비교자에 따라 고유해야 합니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 이nullcomparer 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다.

참고

컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하는 생성자를 사용하면 에 요소를 Dictionary<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

이 생성자는 O(1) 작업입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(Int32)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정한 초기 용량을 갖고 있고 키 형식에 대한 기본 같음 비교자를 사용하는 비어 있는 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(int capacity);
public Dictionary (int capacity);
new System.Collections.Generic.Dictionary<'Key, 'Value> : int -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (capacity As Integer)

매개 변수

capacity
Int32

Dictionary<TKey,TValue>에 포함될 수 있는 초기 요소 수입니다.

예외

capacity 가 0보다 작습니다.

예제

다음 코드 예제에서는 초기 용량이 4인 사전을 만들고 4개의 항목으로 채웁니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys and
        // an initial capacity of 4.
        Dictionary<string, string> openWith =
                               new Dictionary<string, string>(4);

        // Add 4 elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // List the contents of the dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}",
               kvp.Key, kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys and
        ' an initial capacity of 4.
        Dim openWith As New Dictionary(Of String, String)(4)
        
        ' Add 4 elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' List the contents of the dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe

설명

의 모든 키는 Dictionary<TKey,TValue> 기본 같음 비교자에 따라 고유해야 합니다.

의 용량 Dictionary<TKey,TValue> 은 크기 조정이 필요하기 전에 에 Dictionary<TKey,TValue> 추가할 수 있는 요소의 수입니다. 요소가 에 Dictionary<TKey,TValue>추가되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.

컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하면 에 요소를 Dictionary<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다. 또는 매개 변수를 허용하는 생성자를 사용하여 제네릭 인터페이스의 IEqualityComparer<T> 구현을 comparer 지정할 수 있습니다.

이 생성자는 O(1) 작업입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IDictionary<TKey,TValue>을 사용하는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public Dictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue), comparer As IEqualityComparer(Of TKey))

매개 변수

dictionary
IDictionary<TKey,TValue>

요소가 새 IDictionary<TKey,TValue>에 복사되는 Dictionary<TKey,TValue>입니다.

comparer
IEqualityComparer<TKey>

키를 비교할 때 사용할 IEqualityComparer<T> 구현을 지정하거나, 해당 키 형식에 기본 EqualityComparer<T>을 사용하려면 null을 지정합니다.

예외

dictionary이(가) null인 경우

dictionary에 중복 키가 하나 이상 포함된 경우

예제

다음 코드 예제에서는 다른 사전에서 대/소문자를 구분하지 않는 정렬된 콘텐츠를 사용하여 생성자를 사용하여 Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) 를 초기화하는 Dictionary<TKey,TValue> 방법을 보여 줍니다. 코드 예제에서는 대/소문자를 구분하지 않는 비교자를 사용하여 을 만들고 임의 순서로 데이터로 채웁니다. 그런 다음 을 생성자에 전달 SortedDictionary<TKey,TValue>Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) 하고 대/소문자를 구분하지 않는 같음 비교자와 함께 를 전달하여 정렬된 를 Dictionary<TKey,TValue> 만듭니다SortedDictionary<TKey,TValue>. 이는 특정 시점에 정적이 되는 정렬된 사전을 빌드해야 하는 경우에 유용합니다. 에서 SortedDictionary<TKey,TValue> 로 데이터를 복사하면 Dictionary<TKey,TValue> 검색 속도가 향상됩니다.

참고

대/소문자를 구분하지 않는 비교자를 사용하여 새 사전을 만들고 대/소문자 구분 비교자를 사용하는 사전의 항목으로 채우는 경우 이 예제와 같이 입력 사전에 대/소문자만 다른 키가 있는 경우 예외가 발생합니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted dictionary of strings, with string
        // keys and a case-insensitive comparer.
        SortedDictionary<string, string> openWith =
                new SortedDictionary<string, string>(
                    StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("Bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Create a Dictionary of strings with string keys and a
        // case-insensitive equality comparer, and initialize it
        // with the contents of the sorted dictionary.
        Dictionary<string, string> copy =
                new Dictionary<string, string>(openWith,
                    StringComparer.CurrentCultureIgnoreCase);

        // List the contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}",
               kvp.Key, kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted dictionary of strings, with string 
        ' keys and a case-insensitive comparer.
        Dim openWith As New SortedDictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the sorted dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a Dictionary of strings with string keys and a 
        ' case-insensitive equality comparer, and initialize it
        ' with the contents of the sorted dictionary.
        Dim copy As New Dictionary(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

대/소문자를 구분하지 않는 문자열 키를 사용하여 사전을 만들려면 클래스에서 제공하는 StringComparer 대/소문자를 구분하지 않는 문자열 비교자와 함께 이 생성자를 사용합니다.

Dictionary<TKey,TValue> 모든 키는 지정된 비교자에 따라 고유해야 합니다. 마찬가지로 원본 dictionary 의 모든 키도 지정된 비교자에 따라 고유해야 합니다.

참고

예를 들어 가 클래스에서 제공하는 StringComparer 대/소문자를 구분하지 않는 문자열 비교자 중 하나이고 dictionary 대/소문자를 구분하지 않는 비교자 키를 사용하지 않는 경우 comparer 중복 키가 발생할 수 있습니다.

Dictionary<TKey,TValue> 의 초기 용량은 의 모든 요소를 dictionary포함할 수 있을 만큼 큽습니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 이nullcomparer 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다.

이 생성자는 O(n) 작업입니다. 여기서 n 은 의 dictionary요소 수입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정한 Dictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IEnumerable<T>을 사용하는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ collection, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection, System.Collections.Generic.IEqualityComparer<TKey> comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (collection As IEnumerable(Of KeyValuePair(Of TKey, TValue)), comparer As IEqualityComparer(Of TKey))

매개 변수

collection
IEnumerable<KeyValuePair<TKey,TValue>>

요소가 새 IEnumerable<T>에 복사되는 Dictionary<TKey,TValue>입니다.

comparer
IEqualityComparer<TKey>

키를 비교할 때 사용할 IEqualityComparer<T> 구현을 지정하거나, 해당 키 형식에 기본 EqualityComparer<T>을 사용하려면 null을 지정합니다.

예외

collection이(가) null인 경우

collection에 중복 키가 하나 이상 포함된 경우

적용 대상

Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

지정된 초기 용량을 갖고 있고 지정된 Dictionary<TKey,TValue>을 사용하는 비어 있는 IEqualityComparer<T> 클래스의 새 인스턴스를 초기화합니다.

public:
 Dictionary(int capacity, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (int capacity, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public Dictionary (int capacity, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : int * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (capacity As Integer, comparer As IEqualityComparer(Of TKey))

매개 변수

capacity
Int32

Dictionary<TKey,TValue>에 포함될 수 있는 초기 요소 수입니다.

comparer
IEqualityComparer<TKey>

키를 비교할 때 사용할 IEqualityComparer<T> 구현을 지정하거나, 해당 키 형식에 기본 EqualityComparer<T>을 사용하려면 null을 지정합니다.

예외

capacity 가 0보다 작습니다.

예제

다음 코드 예제에서는 초기 용량이 5이고 현재 문화권에 대/소문자를 구분하지 않는 같음 비교자를 사용하여 을 만듭니다 Dictionary<TKey,TValue> . 이 예제에서는 소문자 키가 있는 요소와 대문자 키가 있는 요소 4개를 추가합니다. 그런 다음, 사례별로만 기존 키와 다른 키가 있는 요소를 추가하고 결과 예외를 catch하고 오류 메시지를 표시하는 예제입니다. 마지막으로, 이 예제에서는 사전에 요소를 표시합니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys, an
        // initial capacity of 5, and a case-insensitive equality
        // comparer.
        Dictionary<string, string> openWith =
                      new Dictionary<string, string>(5,
                          StringComparer.CurrentCultureIgnoreCase);

        // Add 4 elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same
        // except for case; this would be allowed with the default
        // comparer.
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the dictionary.");
        }

        // List the contents of the dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

BMP is already in the dictionary.

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys, an
        ' initial capacity of 5, and a case-insensitive equality
        ' comparer.
        Dim openWith As New Dictionary(Of String, String)(5, _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add 4 elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' Try to add a fifth element with a key that is the same 
        ' except for case; this would be allowed with the default
        ' comparer.
        Try
            openWith.Add("BMP", "paint.exe")
        Catch ex As ArgumentException
            Console.WriteLine(vbLf & "BMP is already in the dictionary.")
        End Try
        
        ' List the contents of the dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'BMP is already in the dictionary.
'
'Key = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe

설명

대/소문자를 구분하지 않는 문자열 키를 사용하여 사전을 만들려면 클래스에서 제공하는 StringComparer 대/소문자를 구분하지 않는 문자열 비교자와 함께 이 생성자를 사용합니다.

의 모든 키는 Dictionary<TKey,TValue> 지정된 비교자에 따라 고유해야 합니다.

의 용량 Dictionary<TKey,TValue> 은 크기 조정이 필요하기 전에 에 Dictionary<TKey,TValue> 추가할 수 있는 요소의 수입니다. 요소가 에 Dictionary<TKey,TValue>추가되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.

컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하면 에 요소를 Dictionary<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

Dictionary<TKey,TValue> 는 키가 같은지 여부를 확인하기 위해 같음 구현이 필요합니다. 이 이nullcomparer 이 생성자는 기본 제네릭 같음 비교자 를 EqualityComparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IEquatable<T> 구현하는 경우 기본 같음 비교자는 해당 구현을 사용합니다.

이 생성자는 O(1) 작업입니다.

추가 정보

적용 대상

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)

Source:
Dictionary.cs
Source:
Dictionary.cs
Source:
Dictionary.cs

주의

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

serialize된 데이터를 사용하여 Dictionary<TKey,TValue> 클래스의 새 인스턴스를 초기화합니다.

protected:
 Dictionary(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Dictionary (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected Dictionary (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.Dictionary<'Key, 'Value>
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.Dictionary<'Key, 'Value>
Protected Sub New (info As SerializationInfo, context As StreamingContext)

매개 변수

info
SerializationInfo

SerializationInfo를 serialize하는 데 필요한 정보가 포함된 Dictionary<TKey,TValue> 개체입니다.

context
StreamingContext

StreamingContext와 관련된 serialize된 스트림의 소스와 대상이 들어 있는 Dictionary<TKey,TValue> 구조체입니다.

특성

설명

이 생성자는 역직렬화 중에 호출되어 스트림을 통해 전송되는 개체를 재구성합니다. 자세한 내용은 XML 및 SOAP Serialization합니다.

추가 정보

적용 대상