Assembly : System (dans system.dll)
SyntaxePublic Sub New ( _ dictionary As IDictionary(Of TKey, TValue), _ comparer As IComparer(Of TKey) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim comparer As IComparer(Of TKey) Dim instance As New SortedDictionary(Of TKey, TValue)(dictionary, comparer)
public SortedDictionary (
IDictionary<TKey,TValue> dictionary,
IComparer<TKey> comparer
)
public:
SortedDictionary (
IDictionary<TKey, TValue>^ dictionary,
IComparer<TKey>^ comparer
)
public SortedDictionary (
IDictionary<TKey,TValue> dictionary,
IComparer<TKey> comparer
)
public function SortedDictionary ( dictionary : IDictionary<TKey,TValue>, comparer : IComparer<TKey> )
Paramètres
- dictionary
IDictionary dont les éléments sont copiés dans le nouveau SortedDictionary.
Exceptions
NotesChaque clé d'un SortedDictionary doit être unique selon le comparateur spécifié ; par conséquent, chaque clé du dictionary source doit également être unique selon le comparateur spécifié.
SortedDictionary requiert l'implémentation d'un comparateur pour effectuer des comparaisons de clés. Si comparer est référence Null (Nothing en Visual Basic), ce constructeur utilise le comparateur d'égalité générique par défaut, Comparer.Default. Si le type TKey implémente l'interface générique System.IComparable, le comparateur par défaut utilise cette implémentation.
Ce constructeur est une opération O(n log n), où n est égal au nombre d'éléments de dictionary.
ExempleL'exemple de code suivant montre comment utiliser SortedDictionary pour créer une copie triée sans respect de la casse des informations d'un Dictionary qui ne respecte pas la casse, en passant Dictionary au constructeur SortedDictionary(IDictionary, générique,IComparer, générique). Dans cet exemple, les comparateurs qui ne respectent pas la casse concernent la culture actuelle.
Imports System 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
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 */
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ