SortedList<TKey,TValue> Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy SortedList<TKey,TValue>.

Przeciążenia

SortedList<TKey,TValue>()

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma domyślną pojemność początkową i używa wartości domyślnej IComparer<T>.

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

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma domyślną początkową pojemność i używa określonego IComparer<T>elementu .

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

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, które zawiera elementy skopiowane z określonego IDictionary<TKey,TValue>obiektu , ma wystarczającą pojemność, aby pomieścić liczbę skopiowanych elementów i używa wartości domyślnej IComparer<T>.

SortedList<TKey,TValue>(Int32)

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma określoną początkową pojemność i używa wartości domyślnej IComparer<T>.

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

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, które zawiera elementy skopiowane z określonego IDictionary<TKey,TValue>obiektu , ma wystarczającą pojemność, aby pomieścić liczbę skopiowanych elementów i używa określonego IComparer<T>elementu .

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

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma określoną początkową pojemność i używa określonego IComparer<T>elementu .

SortedList<TKey,TValue>()

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma domyślną pojemność początkową i używa wartości domyślnej IComparer<T>.

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

Przykłady

Poniższy przykład kodu tworzy puste SortedList<TKey,TValue> ciągi z kluczami ciągów i używa Add metody w celu dodania niektórych elementów. W przykładzie Add pokazano, że metoda zgłasza błąd ArgumentException podczas próby dodania zduplikowanego klucza.

Ten przykład kodu jest częścią większego przykładu udostępnionego SortedList<TKey,TValue> dla klasy .

// 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."

Uwagi

Każdy klucz w obiekcie SortedList<TKey,TValue> musi być unikatowy zgodnie z domyślnym modułem porównującym.

Ten konstruktor używa wartości domyślnej dla początkowej pojemności obiektu SortedList<TKey,TValue>. Aby ustawić początkową pojemność, użyj konstruktora SortedList<TKey,TValue>(Int32) . Jeśli można oszacować ostateczny rozmiar kolekcji, określenie początkowej pojemności eliminuje konieczność wykonania wielu operacji zmiany rozmiaru podczas dodawania elementów do elementu SortedList<TKey,TValue>.

Ten konstruktor używa domyślnego porównania dla elementu TKey. Aby określić element porównujący, użyj konstruktora SortedList<TKey,TValue>(IComparer<TKey>) . Domyślny moduł porównujący Comparer<T>.Default sprawdza, czy typ TKey klucza implementuje System.IComparable<T> i używa tej implementacji, jeśli jest dostępna. Jeśli nie, sprawdza, Comparer<T>.Default czy typ TKey klucza implementuje System.IComparableelement . Jeśli typ TKey klucza nie implementuje interfejsu, można określić implementację System.Collections.Generic.IComparer<T> w przeciążeniu konstruktora, który akceptuje comparer parametr.

Ten konstruktor jest operacją O(1).

Zobacz też

Dotyczy

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

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma domyślną początkową pojemność i używa określonego IComparer<T>elementu .

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))

Parametry

comparer
IComparer<TKey>

Implementacja IComparer<T> do użycia podczas porównywania kluczy.

-lub-

null aby użyć wartości domyślnej Comparer<T> dla typu klucza.

Przykłady

Poniższy przykład kodu tworzy posortowaną listę z porównującym bez uwzględniania wielkości liter dla bieżącej kultury. W przykładzie dodano cztery elementy, niektóre z małymi literami i niektóre z wielkimi literami. Następnie próbuje dodać element z kluczem, który różni się od istniejącego klucza tylko według wielkości liter, przechwytuje wynikowy wyjątek i wyświetla komunikat o błędzie. Na koniec przykład wyświetla elementy w kolejności sortowania bez uwzględniania wielkości liter.

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

Uwagi

Każdy klucz w obiekcie SortedList<TKey,TValue> musi być unikatowy zgodnie z określonym modułem porównującym.

Ten konstruktor używa wartości domyślnej dla początkowej pojemności obiektu SortedList<TKey,TValue>. Aby ustawić początkową pojemność, użyj konstruktora SortedList<TKey,TValue>(Int32, IComparer<TKey>) . Jeśli można oszacować ostateczny rozmiar kolekcji, określenie początkowej pojemności eliminuje konieczność wykonania wielu operacji zmiany rozmiaru podczas dodawania elementów do elementu SortedList<TKey,TValue>.

Ten konstruktor jest operacją O(1).

Zobacz też

Dotyczy

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

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, które zawiera elementy skopiowane z określonego IDictionary<TKey,TValue>obiektu , ma wystarczającą pojemność, aby pomieścić liczbę skopiowanych elementów i używa wartości domyślnej 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))

Parametry

dictionary
IDictionary<TKey,TValue>

Element IDictionary<TKey,TValue> , którego elementy są kopiowane do nowego SortedList<TKey,TValue>elementu .

Wyjątki

dictionary to null.

dictionary zawiera co najmniej jeden zduplikowany klucz.

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć SortedList<TKey,TValue> posortowaną kopię informacji w Dictionary<TKey,TValue>obiekcie , przekazując Dictionary<TKey,TValue> element do konstruktora 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

Uwagi

Każdy klucz w elemecie SortedList<TKey,TValue> musi być unikatowy zgodnie z domyślnym modułem porównującym. Podobnie każdy klucz w źródle dictionary musi być unikatowy zgodnie z domyślnym modułem porównującym.

Pojemność nowej SortedList<TKey,TValue> jest ustawiona na liczbę elementów w dictionaryelemecie , więc podczas wypełniania listy nie ma miejsca zmiana rozmiaru.

Ten konstruktor używa domyślnego porównania dla elementu TKey. Aby określić element porównujący, użyj konstruktora SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) . Domyślny moduł porównujący Comparer<T>.Default sprawdza, czy typ TKey klucza implementuje System.IComparable<T> i używa tej implementacji, jeśli jest dostępna. Jeśli nie, sprawdza, Comparer<T>.Default czy typ TKey klucza implementuje System.IComparableelement . Jeśli typ TKey klucza nie implementuje interfejsu, można określić implementację System.Collections.Generic.IComparer<T> w przeciążeniu konstruktora, który akceptuje comparer parametr.

Klucze w dictionary pliku są kopiowane do nowego SortedList<TKey,TValue> i posortowanego raz, co sprawia, że ten konstruktor jest operacją O(n log n).

Zobacz też

Dotyczy

SortedList<TKey,TValue>(Int32)

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma określoną początkową pojemność i używa wartości domyślnej 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)

Parametry

capacity
Int32

Początkowa liczba elementów, które SortedList<TKey,TValue> mogą zawierać.

Wyjątki

Parametr capacity ma wartość niższą niż zero.

Przykłady

Poniższy przykład kodu tworzy posortowaną listę z początkową pojemnością 4 i wypełnia ją 4 wpisami.

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

Uwagi

Każdy klucz w obiekcie SortedList<TKey,TValue> musi być unikatowy zgodnie z domyślnym modułem porównującym.

Pojemność obiektu SortedList<TKey,TValue> to liczba elementów, które mogą być przechowywane przed zmianą rozmiaru SortedList<TKey,TValue> . W miarę SortedList<TKey,TValue>dodawania elementów do obiektu pojemność jest automatycznie zwiększana zgodnie z wymaganiami przez przeniesienie tablicy wewnętrznej.

Jeśli można oszacować rozmiar kolekcji, określenie początkowej pojemności eliminuje konieczność wykonywania wielu operacji zmiany rozmiaru podczas dodawania elementów do elementu SortedList<TKey,TValue>.

Pojemność można zmniejszyć przez wywołanie metody TrimExcess lub jawne Capacity ustawienie właściwości. Zmniejszenie pojemności powoduje cofnięcie przydziału pamięci i skopiowanie wszystkich elementów w obiekcie SortedList<TKey,TValue>.

Ten konstruktor używa domyślnego porównania dla elementu TKey. Aby określić element porównujący, użyj konstruktora SortedList<TKey,TValue>(Int32, IComparer<TKey>) . Domyślny moduł porównujący Comparer<T>.Default sprawdza, czy typ TKey klucza implementuje System.IComparable<T> i używa tej implementacji, jeśli jest dostępna. Jeśli nie, sprawdza, Comparer<T>.Default czy typ TKey klucza implementuje System.IComparableelement . Jeśli typ TKey klucza nie implementuje interfejsu, można określić implementację System.Collections.Generic.IComparer<T> w przeciążeniu konstruktora, który akceptuje comparer parametr.

Ten konstruktor jest operacją O(n), gdzie n to capacity.

Zobacz też

Dotyczy

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

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, które zawiera elementy skopiowane z określonego IDictionary<TKey,TValue>obiektu , ma wystarczającą pojemność, aby pomieścić liczbę skopiowanych elementów i używa określonego IComparer<T>elementu .

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))

Parametry

dictionary
IDictionary<TKey,TValue>

Element IDictionary<TKey,TValue> , którego elementy są kopiowane do nowego SortedList<TKey,TValue>elementu .

comparer
IComparer<TKey>

Implementacja IComparer<T> do użycia podczas porównywania kluczy.

-lub-

null aby użyć wartości domyślnej Comparer<T> dla typu klucza.

Wyjątki

dictionary to null.

dictionary zawiera co najmniej jeden zduplikowany klucz.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą metody SortedList<TKey,TValue> utworzyć posortowaną kopię informacji bez uwzględniania wielkości liter, Dictionary<TKey,TValue>przekazując Dictionary<TKey,TValue> element do konstruktora SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) . W tym przykładzie porównania bez uwzględniania wielkości liter dotyczą bieżącej kultury.

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

Uwagi

Każdy klucz w elemecie SortedList<TKey,TValue> musi być unikatowy zgodnie z określonym modułem porównującym; podobnie każdy klucz w źródle dictionary musi być również unikatowy zgodnie z określonym modułem porównującym.

Pojemność nowej SortedList<TKey,TValue> jest ustawiona na liczbę elementów w dictionaryelemecie , więc podczas wypełniania listy nie ma miejsca zmiana rozmiaru.

Klucze w dictionary pliku są kopiowane do nowego SortedList<TKey,TValue> i posortowanego raz, co sprawia, że ten konstruktor jest operacją O(n log n).

Zobacz też

Dotyczy

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

Źródło:
SortedList.cs
Źródło:
SortedList.cs
Źródło:
SortedList.cs

Inicjuje SortedList<TKey,TValue> nowe wystąpienie klasy, która jest pusta, ma określoną początkową pojemność i używa określonego IComparer<T>elementu .

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))

Parametry

capacity
Int32

Początkowa liczba elementów, które SortedList<TKey,TValue> mogą zawierać.

comparer
IComparer<TKey>

Implementacja IComparer<T> do użycia podczas porównywania kluczy.

-lub-

null aby użyć wartości domyślnej Comparer<T> dla typu klucza.

Wyjątki

Parametr capacity ma wartość niższą niż zero.

Przykłady

Poniższy przykład kodu tworzy posortowaną listę z początkową pojemnością 5 i porównującym bez uwzględniania wielkości liter dla bieżącej kultury. W przykładzie dodano cztery elementy, niektóre z małymi literami i niektóre z wielkimi literami. Następnie próbuje dodać element z kluczem, który różni się od istniejącego klucza tylko według wielkości liter, przechwytuje wynikowy wyjątek i wyświetla komunikat o błędzie. Na koniec przykład wyświetla elementy w kolejności sortowania bez uwzględniania wielkości liter.

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

Uwagi

Każdy klucz w obiekcie SortedList<TKey,TValue> musi być unikatowy zgodnie z określonym modułem porównującym.

Pojemność obiektu SortedList<TKey,TValue> to liczba elementów, które mogą być przechowywane przed zmianą rozmiaru SortedList<TKey,TValue> . W miarę SortedList<TKey,TValue>dodawania elementów do obiektu pojemność jest automatycznie zwiększana zgodnie z wymaganiami przez przeniesienie tablicy wewnętrznej.

Jeśli można oszacować rozmiar kolekcji, określenie początkowej pojemności eliminuje konieczność wykonywania wielu operacji zmiany rozmiaru podczas dodawania elementów do elementu SortedList<TKey,TValue>.

Pojemność można zmniejszyć przez wywołanie metody TrimExcess lub jawne Capacity ustawienie właściwości. Zmniejszenie pojemności powoduje cofnięcie przydziału pamięci i skopiowanie wszystkich elementów w obiekcie SortedList<TKey,TValue>.

Ten konstruktor jest operacją O(n), gdzie n to capacity.

Zobacz też

Dotyczy