SortedDictionary<TKey,TValue> Costruttori

Definizione

Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue>.

Overload

SortedDictionary<TKey,TValue>()

Inizializza una nuova istanza vuota della classe SortedDictionary<TKey,TValue> e usa l'implementazione IComparer<T> predefinita per il tipo di chiave.

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

Inizializza una nuova istanza vuota della classe SortedDictionary<TKey,TValue> e usa l'implementazione IComparer<T> specificata per il confronto delle chiavi.

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

Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue> contenente elementi copiati dalla classe IDictionary<TKey,TValue> specificata e viene usata l'implementazione IComparer<T> predefinita per il tipo di chiave.

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

Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue> contenente elementi copiati dalla classe IDictionary<TKey,TValue> specificata e viene usata l'implementazione IComparer<T> specificata per confrontare le chiavi.

SortedDictionary<TKey,TValue>()

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Inizializza una nuova istanza vuota della classe SortedDictionary<TKey,TValue> e usa l'implementazione IComparer<T> predefinita per il tipo di chiave.

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

Esempio

Nell'esempio di codice seguente viene creato un vuoto SortedDictionary<TKey,TValue> di stringhe con chiavi di stringa e viene usato il Add metodo per aggiungere alcuni elementi. Nell'esempio viene illustrato che il Add metodo genera un oggetto ArgumentException quando si tenta di aggiungere una chiave duplicata.

Questo esempio di codice fa parte di un esempio più grande fornito per 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

Commenti

Ogni chiave in un SortedDictionary<TKey,TValue> deve essere univoca in base al comparer predefinito.

SortedDictionary<TKey,TValue> richiede un'implementazione del comparer per eseguire confronti chiave. Questo costruttore usa il comparer Comparer<T>.Defaultdi uguaglianza generico predefinito . Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, il comparer predefinito usa tale implementazione. In alternativa, è possibile specificare un'implementazione dell'interfaccia IComparer<T> generica usando un costruttore che accetta un comparer parametro.

Questo costruttore è un'operazione O(1).

Vedi anche

Si applica a

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Inizializza una nuova istanza vuota della classe SortedDictionary<TKey,TValue> e usa l'implementazione IComparer<T> specificata per il confronto delle chiavi.

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

Parametri

comparer
IComparer<TKey>

Implementazione di IComparer<T> da usare per confrontare le chiavi oppure null per usare l'oggetto Comparer<T> predefinito per il tipo di chiave.

Esempio

L'esempio di codice seguente crea un oggetto con un SortedDictionary<TKey,TValue> comparer senza distinzione tra maiuscole e minuscole per le impostazioni cultura correnti. Nell'esempio vengono aggiunti quattro elementi, alcuni con chiavi minuscole e alcune con chiavi maiuscole e minuscole. L'esempio tenta quindi di aggiungere un elemento con una chiave diversa da una chiave esistente solo per caso, rileva l'eccezione risultante e visualizza un messaggio di errore. Infine, nell'esempio vengono visualizzati gli elementi in ordine di ordinamento senza distinzione tra maiuscole e minuscole.

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

Commenti

Ogni chiave in un SortedDictionary<TKey,TValue> deve essere univoca in base al comparer specificato.

SortedDictionary<TKey,TValue> richiede un'implementazione del comparer per eseguire confronti chiave. Se comparer è null, questo costruttore usa il comparer di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, il comparer predefinito usa tale implementazione.

Questo costruttore è un'operazione O(1).

Vedi anche

Si applica a

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue> contenente elementi copiati dalla classe IDictionary<TKey,TValue> specificata e viene usata l'implementazione IComparer<T> predefinita per il tipo di chiave.

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

Parametri

dictionary
IDictionary<TKey,TValue>

Oggetto IDictionary<TKey,TValue> i cui elementi sono copiati nel nuovo oggetto SortedDictionary<TKey,TValue>.

Eccezioni

dictionary è null.

dictionary contiene una o più chiavi duplicate.

Esempio

Nell'esempio di codice seguente viene illustrato come creare SortedDictionary<TKey,TValue> una copia ordinata delle informazioni in un Dictionary<TKey,TValue>oggetto , passando l'oggetto Dictionary<TKey,TValue> al SortedDictionary<TKey,TValue>(IComparer<TKey>) costruttore.

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

Commenti

Ogni chiave in un SortedDictionary<TKey,TValue> deve essere univoca in base al comparer predefinito. Pertanto, ogni chiave nell'origine dictionary deve essere univoca in base al comparer predefinito.

SortedDictionary<TKey,TValue> richiede un'implementazione del comparer per eseguire confronti chiave. Questo costruttore usa il comparer di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, il comparer predefinito usa tale implementazione. In alternativa, è possibile specificare un'implementazione dell'interfaccia IComparer<T> generica usando un costruttore che accetta un comparer parametro.

Questo costruttore è un'operazione O(n log n), dove n è il numero di elementi in dictionary.

Vedi anche

Si applica a

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue> contenente elementi copiati dalla classe IDictionary<TKey,TValue> specificata e viene usata l'implementazione IComparer<T> specificata per confrontare le chiavi.

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

Parametri

dictionary
IDictionary<TKey,TValue>

Oggetto IDictionary<TKey,TValue> i cui elementi sono copiati nel nuovo oggetto SortedDictionary<TKey,TValue>.

comparer
IComparer<TKey>

Implementazione di IComparer<T> da usare per confrontare le chiavi oppure null per usare l'oggetto Comparer<T> predefinito per il tipo di chiave.

Eccezioni

dictionary è null.

dictionary contiene una o più chiavi duplicate.

Esempio

Nell'esempio di codice seguente viene illustrato come SortedDictionary<TKey,TValue> creare una copia senza distinzione tra maiuscole e minuscole delle informazioni in caso Dictionary<TKey,TValue>di distinzione tra maiuscole e minuscole passando l'oggetto Dictionary<TKey,TValue> al SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) costruttore. In questo esempio, i comparer senza distinzione tra maiuscole e minuscole sono per le impostazioni cultura correnti.

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

Commenti

Ogni chiave in un SortedDictionary<TKey,TValue> deve essere univoca in base al comparer specificato. Pertanto, ogni chiave nell'origine dictionary deve essere univoca anche in base al comparer specificato.

SortedDictionary<TKey,TValue> richiede un'implementazione del comparer per eseguire confronti chiave. Se comparer è null, questo costruttore usa il comparer di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, il comparer predefinito usa tale implementazione.

Questo costruttore è un'operazione O(n log n), dove n è il numero di elementi in dictionary.

Vedi anche

Si applica a