SortedDictionary<TKey,TValue> Konstruktory

Definicja

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

Przeciążenia

SortedDictionary<TKey,TValue>()

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy, która jest pusta i używa domyślnej IComparer<T> implementacji dla typu klucza.

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy, która jest pusta i używa określonej IComparer<T> implementacji do porównywania kluczy.

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy zawierające elementy skopiowane z określonego IDictionary<TKey,TValue> elementu i używa domyślnej IComparer<T> implementacji dla typu klucza.

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy zawierające elementy skopiowane z określonego IDictionary<TKey,TValue> elementu i używa określonej IComparer<T> implementacji do porównywania kluczy.

SortedDictionary<TKey,TValue>()

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy, która jest pusta i używa domyślnej IComparer<T> implementacji dla typu klucza.

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

Przykłady

Poniższy przykład kodu tworzy puste SortedDictionary<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 SortedDictionary<TKey,TValue> dla klasy .

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

Uwagi

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

SortedDictionary<TKey,TValue> wymaga implementacji porównującej w celu przeprowadzenia kluczowych porównań. Ten konstruktor używa domyślnego ogólnego porównania Comparer<T>.Defaultrówności . Jeśli typ TKey implementuje interfejs ogólny, domyślny moduł porównujący System.IComparable<T> używa tej implementacji. Alternatywnie można określić implementację interfejsu IComparer<T> ogólnego przy użyciu konstruktora, który akceptuje comparer parametr.

Ten konstruktor jest operacją O(1).

Zobacz też

Dotyczy

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

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy, która jest pusta i używa określonej IComparer<T> implementacji do porównywania kluczy.

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

Parametry

comparer
IComparer<TKey>

Implementacja IComparer<T> używana podczas porównywania kluczy lub null używania wartości domyślnej Comparer<T> dla typu klucza.

Przykłady

Poniższy przykład kodu tworzy obiekt SortedDictionary<TKey,TValue> z funkcją porównującą 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 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

Uwagi

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

SortedDictionary<TKey,TValue> wymaga implementacji porównującej w celu przeprowadzenia kluczowych porównań. Jeśli comparer parametr ma nullwartość , ten konstruktor używa domyślnego ogólnego porównania równości, Comparer<T>.Default. Jeśli typ TKey implementuje interfejs ogólny, domyślny moduł porównujący System.IComparable<T> używa tej implementacji.

Ten konstruktor jest operacją O(1).

Zobacz też

Dotyczy

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

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy zawierające elementy skopiowane z określonego IDictionary<TKey,TValue> elementu i używa domyślnej IComparer<T> implementacji dla typu klucza.

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

Parametry

dictionary
IDictionary<TKey,TValue>

Element IDictionary<TKey,TValue> , którego elementy są kopiowane do nowego SortedDictionary<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ć SortedDictionary<TKey,TValue> posortowaną kopię informacji w Dictionary<TKey,TValue>obiekcie , przekazując Dictionary<TKey,TValue> element do konstruktora 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

Uwagi

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

SortedDictionary<TKey,TValue> wymaga implementacji porównującej w celu przeprowadzenia kluczowych porównań. Ten konstruktor używa domyślnego ogólnego porównania równości, Comparer<T>.Default. Jeśli typ TKey implementuje interfejs ogólny, domyślny moduł porównujący System.IComparable<T> używa tej implementacji. Alternatywnie można określić implementację interfejsu IComparer<T> ogólnego przy użyciu konstruktora, który akceptuje comparer parametr.

Ten konstruktor jest operacją O(n log n) , gdzie n jest liczbą elementów w .dictionary

Zobacz też

Dotyczy

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

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

Inicjuje SortedDictionary<TKey,TValue> nowe wystąpienie klasy zawierające elementy skopiowane z określonego IDictionary<TKey,TValue> elementu i używa określonej IComparer<T> implementacji do porównywania kluczy.

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

Parametry

dictionary
IDictionary<TKey,TValue>

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

comparer
IComparer<TKey>

Implementacja IComparer<T> używana podczas porównywania kluczy lub null używania 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 SortedDictionary<TKey,TValue> utworzyć posortowaną kopię informacji bez uwzględniania wielkości liter, Dictionary<TKey,TValue>przekazując Dictionary<TKey,TValue> element do konstruktora SortedDictionary<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");

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

Uwagi

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

SortedDictionary<TKey,TValue> wymaga implementacji porównującej w celu przeprowadzenia kluczowych porównań. Jeśli comparer parametr ma nullwartość , ten konstruktor używa domyślnego ogólnego porównania równości, Comparer<T>.Default. Jeśli typ TKey implementuje interfejs ogólny, domyślny moduł porównujący System.IComparable<T> używa tej implementacji.

Ten konstruktor jest operacją O(n log n) , gdzie n jest liczbą elementów w .dictionary

Zobacz też

Dotyczy