SortedDictionary<TKey,TValue> 생성자

정의

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

오버로드

SortedDictionary<TKey,TValue>()

비어 있고 키 형식에 대해 기본적으로 구현된 SortedDictionary<TKey,TValue>를 사용하는 빈 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

SortedDictionary<TKey,TValue>(IComparer<TKey>)

지정된 SortedDictionary<TKey,TValue> 구현을 사용하여 키를 비교하는 빈 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

지정한 SortedDictionary<TKey,TValue>에서 복사된 요소를 포함하고 키 형식에 대해 기본적으로 구현된 IDictionary<TKey,TValue>를 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

지정한 SortedDictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IDictionary<TKey,TValue> 구현을 사용하여 키를 비교하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

SortedDictionary<TKey,TValue>()

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

비어 있고 키 형식에 대해 기본적으로 구현된 SortedDictionary<TKey,TValue>를 사용하는 빈 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

예제

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

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

// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
    new SortedDictionary<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 sorted dictionary of strings, with string 
' keys. 
Dim openWith As New SortedDictionary(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

설명

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

SortedDictionary<TKey,TValue> 키 비교를 수행하려면 비교자 구현이 필요합니다. 이 생성자는 기본 제네릭 같음 비교자 Comparer<T>.Default를 사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IComparable<T> 구현하는 경우 기본 비교자는 해당 구현을 사용합니다. 또는 매개 변수를 허용하는 생성자를 사용하여 제네릭 인터페이스의 IComparer<T> 구현을 comparer 지정할 수 있습니다.

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

추가 정보

적용 대상

SortedDictionary<TKey,TValue>(IComparer<TKey>)

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

지정된 SortedDictionary<TKey,TValue> 구현을 사용하여 키를 비교하는 빈 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

매개 변수

comparer
IComparer<TKey>

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

예제

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

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new SortedDictionary of strings, with string keys
        // and a case-insensitive comparer for the current culture.
        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");

        // 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 = 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 SortedDictionary of strings, with string keys 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New SortedDictionary(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 sorted 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 = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

SortedDictionary<TKey,TValue> 키 비교를 수행하려면 비교자 구현이 필요합니다. 이 이nullcomparer 이 생성자는 기본 제네릭 같음 비교자 를 Comparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IComparable<T> 구현하는 경우 기본 비교자는 해당 구현을 사용합니다.

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

추가 정보

적용 대상

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

지정한 SortedDictionary<TKey,TValue>에서 복사된 요소를 포함하고 키 형식에 대해 기본적으로 구현된 IDictionary<TKey,TValue>를 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

매개 변수

dictionary
IDictionary<TKey,TValue>

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

예외

dictionary이(가) null인 경우

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

예제

다음 코드 예제에서는 를 사용하여 SortedDictionary<TKey,TValue> 생성자에 를 전달 Dictionary<TKey,TValue> 하여 에서 Dictionary<TKey,TValue>정보의 정렬된 복사본을 SortedDictionary<TKey,TValue>(IComparer<TKey>) 만드는 방법을 보여줍니다.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith =
                                  new Dictionary<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 SortedDictionary of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                  new SortedDictionary<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 Dictionary of strings, with string 
        ' keys.
        Dim openWith As New Dictionary(Of 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 SortedDictionary of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(Of String, String)(openWith)

        ' List the sorted 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

설명

SortedDictionary<TKey,TValue> 모든 키는 기본 비교자에 따라 고유해야 하므로 원본 dictionary 의 모든 키도 기본 비교자에 따라 고유해야 합니다.

SortedDictionary<TKey,TValue> 키 비교를 수행하려면 비교자 구현이 필요합니다. 이 생성자는 기본 제네릭 같음 비교자 를 Comparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IComparable<T> 구현하는 경우 기본 비교자는 해당 구현을 사용합니다. 또는 매개 변수를 허용하는 생성자를 사용하여 제네릭 인터페이스의 IComparer<T> 구현을 comparer 지정할 수 있습니다.

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

추가 정보

적용 대상

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

지정한 SortedDictionary<TKey,TValue>에서 복사된 요소를 포함하고 지정한 IDictionary<TKey,TValue> 구현을 사용하여 키를 비교하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

매개 변수

dictionary
IDictionary<TKey,TValue>

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

comparer
IComparer<TKey>

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

예외

dictionary이(가) null인 경우

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

예제

다음 코드 예제에서는 를 사용하여 SortedDictionary<TKey,TValue> 대/소문자를 구분하지 않는 에서 대/소문자를 구분하지 않는 정렬된 정보의 Dictionary<TKey,TValue>복사본을 생성자에 전달 Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 하여 만드는 방법을 보여 줍니다. 이 예제에서 대/소문자를 구분하지 않는 비교자는 현재 문화권에 대한 것입니다.

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 equality 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");

        // 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);
        }

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

        // List the sorted 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 = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

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 Dictionary of strings, with string keys and
        ' a case-insensitive equality 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")
        
        ' 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

        ' Create a SortedDictionary of strings with string keys and a 
        ' case-insensitive equality comparer for the current culture,
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the sorted 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 = txt, Value = notepad.exe
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

SortedDictionary<TKey,TValue> 키 비교를 수행하려면 비교자 구현이 필요합니다. 이 이nullcomparer 이 생성자는 기본 제네릭 같음 비교자 를 Comparer<T>.Default사용합니다. 형식 TKey 이 제네릭 인터페이스를 System.IComparable<T> 구현하는 경우 기본 비교자는 해당 구현을 사용합니다.

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

추가 정보

적용 대상