SortedDictionary<TKey,TValue> Constructors

Definitie

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse.

Overloads

Name Description
SortedDictionary<TKey,TValue>()

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die leeg is en gebruikt de standaard IComparer<T> implementatie voor het sleuteltype.

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die leeg is en gebruikt de opgegeven IComparer<T> implementatie om sleutels te vergelijken.

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de standaard IComparer<T> implementatie voor het sleuteltype.

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de opgegeven IComparer<T> implementatie om sleutels te vergelijken.

SortedDictionary<TKey,TValue>()

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die leeg is en gebruikt de standaard IComparer<T> implementatie voor het sleuteltype.

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

Voorbeelden

In het volgende codevoorbeeld wordt een lege SortedDictionary<TKey,TValue> tekenreeks met tekenreekssleutels gemaakt en wordt de Add methode gebruikt om enkele elementen toe te voegen. In het voorbeeld ziet u dat de Add methode een ArgumentException dubbele sleutel genereert bij het toevoegen van een dubbele sleutel.

Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de SortedDictionary<TKey,TValue> klasse.

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

Opmerkingen

Elke sleutel in een SortedDictionary<TKey,TValue> moet uniek zijn volgens de standaard comparer.

SortedDictionary<TKey,TValue> vereist een comparer-implementatie om belangrijke vergelijkingen uit te voeren. Deze constructor maakt gebruik van de standaard algemene gelijkheidsgelijker Comparer<T>.Default. Als het type TKey de System.IComparable<T> algemene interface implementeert, gebruikt de standaard-vergelijkingsfunctie die implementatie. U kunt ook een implementatie van de IComparer<T> algemene interface opgeven met behulp van een constructor die een comparer parameter accepteert.

Deze constructor is een O(1)-bewerking.

Zie ook

Van toepassing op

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die leeg is en gebruikt de opgegeven IComparer<T> implementatie om sleutels te vergelijken.

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

Parameters

comparer
IComparer<TKey>

De IComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde Comparer<T> voor het type sleutel.

Voorbeelden

In het volgende codevoorbeeld wordt een SortedDictionary<TKey,TValue> met een hoofdlettergevoelige vergelijking voor de huidige cultuur gemaakt. In het voorbeeld worden vier elementen toegevoegd, sommige met kleine letters en sommige met hoofdletters. Het voorbeeld probeert vervolgens een element toe te voegen met een sleutel die alleen per geval verschilt van een bestaande sleutel, de resulterende uitzondering onderschept en een foutbericht weergeeft. Ten slotte geeft het voorbeeld de elementen weer in hoofdlettergevoelige sorteervolgorde.

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

Opmerkingen

Elke sleutel in een SortedDictionary<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie.

SortedDictionary<TKey,TValue> vereist een comparer-implementatie om belangrijke vergelijkingen uit te voeren. Als comparer dat het is null, gebruikt deze constructor de standaard algemene gelijkheidsgelijker, Comparer<T>.Default. Als het type TKey de System.IComparable<T> algemene interface implementeert, gebruikt de standaard-vergelijkingsfunctie die implementatie.

Deze constructor is een O(1)-bewerking.

Zie ook

Van toepassing op

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de standaard IComparer<T> implementatie voor het sleuteltype.

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

Parameters

dictionary
IDictionary<TKey,TValue>

De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe SortedDictionary<TKey,TValue>worden gekopieerd.

Uitzonderingen

dictionary is null.

dictionary bevat een of meer dubbele sleutels.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe SortedDictionary<TKey,TValue> u een gesorteerde kopie van de informatie in een Dictionary<TKey,TValue>maakt door de Dictionary<TKey,TValue> constructor door te geven aan de SortedDictionary<TKey,TValue>(IComparer<TKey>) constructor.

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

Opmerkingen

Elke sleutel in een SortedDictionary<TKey,TValue> moet uniek zijn volgens de standaard comparer. Daarom moet elke sleutel in de bron dictionary ook uniek zijn volgens de standaard-vergelijkingsfunctie.

SortedDictionary<TKey,TValue> vereist een comparer-implementatie om belangrijke vergelijkingen uit te voeren. Deze constructor maakt gebruik van de standaard algemene gelijkheids comparer, Comparer<T>.Default. Als het type TKey de System.IComparable<T> algemene interface implementeert, gebruikt de standaard-vergelijkingsfunctie die implementatie. U kunt ook een implementatie van de IComparer<T> algemene interface opgeven met behulp van een constructor die een comparer parameter accepteert.

Deze constructor is een O(n logboek n) bewerking, waarbij n het aantal elementen in dictionary.

Zie ook

Van toepassing op

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

Initialiseert een nieuw exemplaar van de SortedDictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de opgegeven IComparer<T> implementatie om sleutels te vergelijken.

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

Parameters

dictionary
IDictionary<TKey,TValue>

De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe SortedDictionary<TKey,TValue>worden gekopieerd.

comparer
IComparer<TKey>

De IComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde Comparer<T> voor het type sleutel.

Uitzonderingen

dictionary is null.

dictionary bevat een of meer dubbele sleutels.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe SortedDictionary<TKey,TValue> u een niet-hoofdlettergevoelige, gesorteerde kopie van de informatie maakt in een niet-hoofdlettergevoelige Dictionary<TKey,TValue>, door de Dictionary<TKey,TValue> aan de SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructor door te geven. In dit voorbeeld zijn de hoofdlettergevoelige vergelijkingen voor de huidige cultuur.

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

Opmerkingen

Elke sleutel in een SortedDictionary<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie. Daarom moet elke sleutel in de bron dictionary ook uniek zijn volgens de opgegeven vergelijkingsfunctie.

SortedDictionary<TKey,TValue> vereist een comparer-implementatie om belangrijke vergelijkingen uit te voeren. Als comparer dat het is null, gebruikt deze constructor de standaard algemene gelijkheidsgelijker, Comparer<T>.Default. Als het type TKey de System.IComparable<T> algemene interface implementeert, gebruikt de standaard-vergelijkingsfunctie die implementatie.

Deze constructor is een O(n logboek n) bewerking, waarbij n het aantal elementen in dictionary.

Zie ook

Van toepassing op