SortedDictionary<TKey,TValue> Constructeurs

Définition

Initialise une nouvelle instance de la classe SortedDictionary<TKey,TValue>.

Surcharges

Nom Description
SortedDictionary<TKey,TValue>()

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe vide et utilise l’implémentation par défaut IComparer<T> pour le type de clé.

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe vide et utilise l’implémentation spécifiée IComparer<T> pour comparer les clés.

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe qui contient des éléments copiés à partir du spécifié IDictionary<TKey,TValue> et utilise l’implémentation par défaut IComparer<T> pour le type de clé.

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe qui contient des éléments copiés à partir de l’implémentation spécifiée IDictionary<TKey,TValue> et utilise l’implémentation spécifiée IComparer<T> pour comparer les clés.

SortedDictionary<TKey,TValue>()

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe vide et utilise l’implémentation par défaut IComparer<T> pour le type de clé.

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

Exemples

L’exemple de code suivant crée une chaîne vide SortedDictionary<TKey,TValue> avec des clés de chaîne et utilise la Add méthode pour ajouter des éléments. L’exemple montre que la Add méthode lève une ArgumentException exception lors de la tentative d’ajout d’une clé en double.

Cet exemple de code fait partie d’un exemple plus grand fourni pour la SortedDictionary<TKey,TValue> classe.

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

Remarques

Chaque clé d’un SortedDictionary<TKey,TValue> doit être unique en fonction du comparateur par défaut.

SortedDictionary<TKey,TValue> nécessite une implémentation de comparateur pour effectuer des comparaisons clés. Ce constructeur utilise le comparateur Comparer<T>.Defaultd’égalité générique par défaut. Si le type TKey implémente l’interface System.IComparable<T> générique, le comparateur par défaut utilise cette implémentation. Vous pouvez également spécifier une implémentation de l’interface générique à l’aide IComparer<T> d’un constructeur qui accepte un comparer paramètre.

Ce constructeur est une opération O(1).

Voir aussi

S’applique à

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe vide et utilise l’implémentation spécifiée IComparer<T> pour comparer les clés.

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

Paramètres

comparer
IComparer<TKey>

Implémentation IComparer<T> à utiliser lors de la comparaison des clés ou null pour utiliser la valeur par défaut Comparer<T> pour le type de la clé.

Exemples

L’exemple de code suivant crée un SortedDictionary<TKey,TValue> comparateur sans respect de la casse pour la culture actuelle. L’exemple ajoute quatre éléments, certains avec des clés minuscules et certains avec des clés majuscules. L’exemple tente ensuite d’ajouter un élément avec une clé qui diffère d’une clé existante uniquement par cas, intercepte l’exception résultante et affiche un message d’erreur. Enfin, l’exemple affiche les éléments dans l’ordre de tri ne respectant pas la casse.

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

Remarques

Chaque clé d’un SortedDictionary<TKey,TValue> doit être unique en fonction du comparateur spécifié.

SortedDictionary<TKey,TValue> nécessite une implémentation de comparateur pour effectuer des comparaisons clés. Si comparer c’est nullle cas, ce constructeur utilise le comparateur d’égalité générique par défaut. Comparer<T>.Default Si le type TKey implémente l’interface System.IComparable<T> générique, le comparateur par défaut utilise cette implémentation.

Ce constructeur est une opération O(1).

Voir aussi

S’applique à

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe qui contient des éléments copiés à partir du spécifié IDictionary<TKey,TValue> et utilise l’implémentation par défaut IComparer<T> pour le type de clé.

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

Paramètres

dictionary
IDictionary<TKey,TValue>

Dont IDictionary<TKey,TValue> les éléments sont copiés dans le nouveau SortedDictionary<TKey,TValue>.

Exceptions

dictionary a la valeur null.

dictionary contient une ou plusieurs clés dupliquées.

Exemples

L’exemple de code suivant montre comment utiliser SortedDictionary<TKey,TValue> pour créer une copie triée des informations dans un Dictionary<TKey,TValue>, en passant le Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IComparer<TKey>) constructeur.

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

Remarques

Chaque clé d’un SortedDictionary<TKey,TValue> doit être unique en fonction du comparateur par défaut ; par conséquent, chaque clé de la source dictionary doit également être unique en fonction du comparateur par défaut.

SortedDictionary<TKey,TValue> nécessite une implémentation de comparateur pour effectuer des comparaisons clés. Ce constructeur utilise le comparateur d’égalité générique par défaut. Comparer<T>.Default Si le type TKey implémente l’interface System.IComparable<T> générique, le comparateur par défaut utilise cette implémentation. Vous pouvez également spécifier une implémentation de l’interface générique à l’aide IComparer<T> d’un constructeur qui accepte un comparer paramètre.

Ce constructeur est une opération O(n log n), où n est le nombre d’éléments dans dictionary.

Voir aussi

S’applique à

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

Initialise une nouvelle instance de la SortedDictionary<TKey,TValue> classe qui contient des éléments copiés à partir de l’implémentation spécifiée IDictionary<TKey,TValue> et utilise l’implémentation spécifiée IComparer<T> pour comparer les clés.

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

Paramètres

dictionary
IDictionary<TKey,TValue>

Dont IDictionary<TKey,TValue> les éléments sont copiés dans le nouveau SortedDictionary<TKey,TValue>.

comparer
IComparer<TKey>

Implémentation IComparer<T> à utiliser lors de la comparaison des clés ou null pour utiliser la valeur par défaut Comparer<T> pour le type de la clé.

Exceptions

dictionary a la valeur null.

dictionary contient une ou plusieurs clés dupliquées.

Exemples

L’exemple de code suivant montre comment utiliser SortedDictionary<TKey,TValue> pour créer une copie triée sans respect de la casse des informations dans une non-respect Dictionary<TKey,TValue>de la casse, en passant le Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructeur. Dans cet exemple, les comparateurs ne respectant pas la casse concernent la culture actuelle.

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

Remarques

Chaque clé d’un SortedDictionary<TKey,TValue> doit être unique en fonction du comparateur spécifié ; par conséquent, chaque clé de la source dictionary doit également être unique en fonction du comparateur spécifié.

SortedDictionary<TKey,TValue> nécessite une implémentation de comparateur pour effectuer des comparaisons clés. Si comparer c’est nullle cas, ce constructeur utilise le comparateur d’égalité générique par défaut. Comparer<T>.Default Si le type TKey implémente l’interface System.IComparable<T> générique, le comparateur par défaut utilise cette implémentation.

Ce constructeur est une opération O(n log n), où n est le nombre d’éléments dans dictionary.

Voir aussi

S’applique à