SortedList<TKey,TValue> 생성자

정의

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

오버로드

SortedList<TKey,TValue>()

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

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

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

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

지정된 SortedList<TKey,TValue>에서 복사된 요소가 포함되어 있고, 복사된 요소의 수를 수용할 수 있는 충분한 용량을 가지며, 기본 IDictionary<TKey,TValue>을 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

SortedList<TKey,TValue>(Int32)

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

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

지정된 SortedList<TKey,TValue>에서 복사된 요소가 포함되어 있고, 복사된 요소의 수를 수용할 수 있는 충분한 용량을 가지며, 지정된 IDictionary<TKey,TValue>을 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

SortedList<TKey,TValue>(Int32, IComparer<TKey>)

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

SortedList<TKey,TValue>()

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

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

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

예제

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

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

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

// Add some elements to the list. 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 list.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
    new SortedList<string, string>();

// Add some elements to the list. 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 list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of String, String)

' Add some elements to the list. 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 list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()

// Add some elements to the list. 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 list.
try
    openWith.Add("txt", "winword.exe");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

설명

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

이 생성자는 의 초기 용량에 기본값을 SortedList<TKey,TValue>사용합니다. 초기 용량을 설정하려면 생성자를 SortedList<TKey,TValue>(Int32) 사용합니다. 컬렉션의 최종 크기를 예측할 수 있는 경우 초기 용량을 지정하면 에 요소를 SortedList<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

이 생성자는 에 대한 TKey기본 비교자를 사용합니다. 비교자를 지정하려면 생성자를 SortedList<TKey,TValue>(IComparer<TKey>) 사용합니다. 기본 비교자는 Comparer<T>.Default 키 형식 TKey 이 구현하고 사용 가능한 경우 해당 구현을 사용하는지 여부를 확인합니다 System.IComparable<T> . 그렇지 않은 Comparer<T>.Default 경우 키 형식 TKey 이 를 구현하는지 여부를 확인합니다.System.IComparable 키 형식 TKey 이 두 인터페이스 중 하나를 구현하지 않는 경우 매개 변수를 System.Collections.Generic.IComparer<T> 허용하는 생성자 오버로드에서 구현을 comparer 지정할 수 있습니다.

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

추가 정보

적용 대상

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

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

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

매개 변수

comparer
IComparer<TKey>

키를 비교할 때 사용하는 IComparer<T> 구현입니다.

또는

null이면 기본 Comparer<T>을 키의 형식으로 사용합니다.

예제

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

using System;
using System.Collections.Generic;

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

        // Add some elements to the list.
        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 sorted list.");
        }

        // List the contents of the sorted list.
        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 sorted list.

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 list of strings, with string keys and
        ' a case-insensitive comparer for the current culture.
        Dim openWith As New SortedList(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the list. 
        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 sorted list.")
        End Try
        
        ' List the contents of the sorted list.
        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 sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

이 생성자는 의 초기 용량에 기본값을 SortedList<TKey,TValue>사용합니다. 초기 용량을 설정하려면 생성자를 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 사용합니다. 컬렉션의 최종 크기를 예측할 수 있는 경우 초기 용량을 지정하면 에 요소를 SortedList<TKey,TValue>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.

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

추가 정보

적용 대상

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

지정된 SortedList<TKey,TValue>에서 복사된 요소가 포함되어 있고, 복사된 요소의 수를 수용할 수 있는 충분한 용량을 가지며, 기본 IDictionary<TKey,TValue>을 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

매개 변수

dictionary
IDictionary<TKey,TValue>

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

예외

dictionary이(가) null인 경우

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

예제

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

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 SortedList of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
                  new SortedList<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 SortedList of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedList(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

설명

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

SortedList<TKey,TValue> 의 용량은 의 요소 dictionary수로 설정되므로 목록이 채워지는 동안 크기 조정이 발생하지 않습니다.

이 생성자는 에 대한 TKey기본 비교자를 사용합니다. 비교자를 지정하려면 생성자를 SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 사용합니다. 기본 비교자는 Comparer<T>.Default 키 형식 TKey 이 구현하고 사용 가능한 경우 해당 구현을 사용하는지 여부를 확인합니다 System.IComparable<T> . 그렇지 않은 Comparer<T>.Default 경우 키 형식 TKey 이 를 구현하는지 여부를 확인합니다.System.IComparable 키 형식 TKey 이 두 인터페이스 중 하나를 구현하지 않는 경우 매개 변수를 System.Collections.Generic.IComparer<T> 허용하는 생성자 오버로드에서 구현을 comparer 지정할 수 있습니다.

dictionary 키는 새 SortedList<TKey,TValue> 에 복사되고 한 번 정렬되어 이 생성자를 O(n 로그 n) 작업으로 만듭니다.

추가 정보

적용 대상

SortedList<TKey,TValue>(Int32)

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

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

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

매개 변수

capacity
Int32

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

예외

capacity가 0보다 작은 경우

예제

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

using System;
using System.Collections.Generic;

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

        // Add 4 elements to the list.
        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 sorted list.
        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 = 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 list of strings, with string keys and
        ' an initial capacity of 4.
        Dim openWith As New SortedList(Of String, String)(4)
        
        ' Add 4 elements to the list. 
        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 sorted list.
        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 = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

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

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

를 호출 TrimExcess 하거나 속성을 명시적으로 설정하여 용량을 Capacity 줄일 수 있습니다. 용량을 줄이면 메모리가 다시 할당되고 의 모든 요소가 SortedList<TKey,TValue>복사됩니다.

이 생성자는 에 대한 TKey기본 비교자를 사용합니다. 비교자를 지정하려면 생성자를 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 사용합니다. 기본 비교자는 Comparer<T>.Default 키 형식 TKey 이 구현하고 사용 가능한 경우 해당 구현을 사용하는지 여부를 확인합니다 System.IComparable<T> . 그렇지 않은 Comparer<T>.Default 경우 키 형식 TKey 이 를 구현하는지 여부를 확인합니다.System.IComparable 키 형식 TKey 이 두 인터페이스 중 하나를 구현하지 않는 경우 매개 변수를 System.Collections.Generic.IComparer<T> 허용하는 생성자 오버로드에서 구현을 comparer 지정할 수 있습니다.

이 생성자는 O(n) 작업이며 여기서 n 는 입니다 capacity.

추가 정보

적용 대상

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

지정된 SortedList<TKey,TValue>에서 복사된 요소가 포함되어 있고, 복사된 요소의 수를 수용할 수 있는 충분한 용량을 가지며, 지정된 IDictionary<TKey,TValue>을 사용하는 IComparer<T> 클래스의 새 인스턴스를 초기화합니다.

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

매개 변수

dictionary
IDictionary<TKey,TValue>

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

comparer
IComparer<TKey>

키를 비교할 때 사용하는 IComparer<T> 구현입니다.

또는

null이면 기본 Comparer<T>을 키의 형식으로 사용합니다.

예외

dictionary이(가) null인 경우

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

예제

다음 코드 예제에서는 를 사용하여 SortedList<TKey,TValue> 대/소문자를 구분하지 않는 에서 대/소문자를 구분하지 않는 정렬된 정보의 Dictionary<TKey,TValue>복사본을 생성자에 전달 Dictionary<TKey,TValue>SortedList<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");

        // Create a SortedList of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
            new SortedList<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 = 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")
        
        ' Create a SortedList 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 SortedList(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 = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

SortedList<TKey,TValue> 의 용량은 의 요소 dictionary수로 설정되므로 목록이 채워지는 동안 크기 조정이 발생하지 않습니다.

dictionary 키는 새 SortedList<TKey,TValue> 에 복사되고 한 번 정렬되어 이 생성자를 O(n 로그 n) 작업으로 만듭니다.

추가 정보

적용 대상

SortedList<TKey,TValue>(Int32, IComparer<TKey>)

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

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

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

매개 변수

capacity
Int32

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

comparer
IComparer<TKey>

키를 비교할 때 사용하는 IComparer<T> 구현입니다.

또는

null이면 기본 Comparer<T>을 키의 형식으로 사용합니다.

예외

capacity가 0보다 작은 경우

예제

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

using System;
using System.Collections.Generic;

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

        // Add 4 elements to the list.
        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 sorted list.");
        }

        // List the contents of the sorted list.
        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 sorted list.

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 list of strings, with string keys, an
        ' initial capacity of 5, and a case-insensitive comparer.
        Dim openWith As New SortedList(Of String, String)(5, _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add 4 elements to the list. 
        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 sorted list.")
        End Try
        
        ' List the contents of the sorted list.
        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 sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

설명

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

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

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

를 호출 TrimExcess 하거나 속성을 명시적으로 설정하여 용량을 Capacity 줄일 수 있습니다. 용량을 줄이면 메모리가 다시 할당되고 의 모든 요소가 SortedList<TKey,TValue>복사됩니다.

이 생성자는 O(n) 작업이며 여기서 n 는 입니다 capacity.

추가 정보

적용 대상