SortedDictionary<TKey,TValue> Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe SortedDictionary<TKey,TValue>.
Overload
| Nome | Descrizione |
|---|---|
| SortedDictionary<TKey,TValue>() |
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe vuota e usa l'implementazione predefinita IComparer<T> per il tipo di chiave. |
| SortedDictionary<TKey,TValue>(IComparer<TKey>) |
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe vuota e usa l'implementazione specificata IComparer<T> per confrontare le chiavi. |
| SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>) |
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe che contiene elementi copiati dall'oggetto specificato IDictionary<TKey,TValue> e usa l'implementazione predefinita IComparer<T> per il tipo di chiave. |
| SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe che contiene elementi copiati dall'oggetto specificato IDictionary<TKey,TValue> e usa l'implementazione specificata IComparer<T> per confrontare le chiavi. |
SortedDictionary<TKey,TValue>()
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe vuota e usa l'implementazione predefinita IComparer<T> 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 stringa e viene usato il Add metodo per aggiungere alcuni elementi. Nell'esempio viene illustrato che il Add metodo genera un'eccezione ArgumentException quando si tenta di aggiungere una chiave duplicata.
Questo esempio di codice fa parte di un esempio più ampio 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 di un SortedDictionary<TKey,TValue> oggetto deve essere univoca in base all'operatore di confronto predefinito.
SortedDictionary<TKey,TValue> richiede un'implementazione dell'operatore di confronto per eseguire confronti chiave. Questo costruttore usa l'operatore di confronto Comparer<T>.Defaultdi uguaglianza generico predefinito. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, l'operatore di confronto 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>)
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe vuota e usa l'implementazione specificata IComparer<T> per confrontare le chiavi.
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 IComparer<T> da usare per il confronto delle chiavi o null per usare l'impostazione predefinita Comparer<T> per il tipo di chiave.
Esempio
Nell'esempio di codice seguente viene creato un SortedDictionary<TKey,TValue> oggetto con un operatore di confronto 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. L'esempio tenta quindi di aggiungere un elemento con una chiave diversa da una chiave esistente solo per caso, intercetta 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 di un SortedDictionary<TKey,TValue> oggetto deve essere univoca in base all'operatore di confronto specificato.
SortedDictionary<TKey,TValue> richiede un'implementazione dell'operatore di confronto per eseguire confronti chiave. Se comparer è null, questo costruttore usa l'operatore di confronto di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, l'operatore di confronto predefinito usa tale implementazione.
Questo costruttore è un'operazione O(1).
Vedi anche
Si applica a
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe che contiene elementi copiati dall'oggetto specificato IDictionary<TKey,TValue> e usa l'implementazione predefinita IComparer<T> 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 i IDictionary<TKey,TValue> cui elementi vengono copiati nel nuovo SortedDictionary<TKey,TValue>oggetto .
Eccezioni
dictionary è null.
dictionary contiene una o più chiavi duplicate.
Esempio
Nell'esempio di codice seguente viene illustrato come usare SortedDictionary<TKey,TValue> per creare 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 di un SortedDictionary<TKey,TValue> oggetto deve essere univoca in base all'operatore di confronto predefinito. Pertanto, ogni chiave nell'origine dictionary deve essere univoca anche in base all'operatore di confronto predefinito.
SortedDictionary<TKey,TValue> richiede un'implementazione dell'operatore di confronto per eseguire confronti chiave. Questo costruttore usa l'operatore di confronto di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, l'operatore di confronto 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 ), dove n è il numero di elementi in dictionaryn.
Vedi anche
Si applica a
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
Inizializza una nuova istanza della SortedDictionary<TKey,TValue> classe che contiene elementi copiati dall'oggetto specificato IDictionary<TKey,TValue> e usa l'implementazione specificata IComparer<T> 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);
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 i IDictionary<TKey,TValue> cui elementi vengono copiati nel nuovo SortedDictionary<TKey,TValue>oggetto .
- comparer
- IComparer<TKey>
Implementazione IComparer<T> da usare per il confronto delle chiavi o null per usare l'impostazione predefinita Comparer<T> per il tipo di chiave.
Eccezioni
dictionary è null.
dictionary contiene una o più chiavi duplicate.
Esempio
Nell'esempio di codice seguente viene illustrato come usare SortedDictionary<TKey,TValue> per creare una copia ordinata senza distinzione tra maiuscole e minuscole delle informazioni in un oggetto senza distinzione Dictionary<TKey,TValue>tra maiuscole e minuscole passando l'oggetto Dictionary<TKey,TValue> al SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) costruttore. In questo esempio, gli strumenti di confronto senza distinzione tra maiuscole e minuscole sono destinati alle 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 di un SortedDictionary<TKey,TValue> oggetto deve essere univoca in base all'operatore di confronto specificato. Pertanto, ogni chiave nell'origine dictionary deve essere univoca anche in base all'operatore di confronto specificato.
SortedDictionary<TKey,TValue> richiede un'implementazione dell'operatore di confronto per eseguire confronti chiave. Se comparer è null, questo costruttore usa l'operatore di confronto di uguaglianza generico predefinito, Comparer<T>.Default. Se il tipo TKey implementa l'interfaccia System.IComparable<T> generica, l'operatore di confronto predefinito usa tale implementazione.
Questo costruttore è un'operazione O(n log ), dove n è il numero di elementi in dictionaryn.