SortedDictionary<TKey,TValue> Konstruktoren
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Initialisiert eine neue Instanz der SortedDictionary<TKey,TValue>-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| SortedDictionary<TKey,TValue>() |
Initialisiert eine neue Instanz der Klasse, die SortedDictionary<TKey,TValue> leer ist, und verwendet die Standardimplementierung IComparer<T> für den Schlüsseltyp. |
| SortedDictionary<TKey,TValue>(IComparer<TKey>) |
Initialisiert eine neue Instanz der Klasse, die SortedDictionary<TKey,TValue> leer ist, und verwendet die angegebene IComparer<T> Implementierung zum Vergleichen von Schlüsseln. |
| SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>) |
Initialisiert eine neue Instanz der SortedDictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die Standardimplementierung IComparer<T> für den Schlüsseltyp. |
| SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
Initialisiert eine neue Instanz der SortedDictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die angegebene IComparer<T> Implementierung zum Vergleichen von Schlüsseln. |
SortedDictionary<TKey,TValue>()
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
Initialisiert eine neue Instanz der Klasse, die SortedDictionary<TKey,TValue> leer ist, und verwendet die Standardimplementierung IComparer<T> für den Schlüsseltyp.
public:
SortedDictionary();
public SortedDictionary();
Public Sub New ()
Beispiele
Im folgenden Codebeispiel wird eine leere SortedDictionary<TKey,TValue> Zeichenfolge mit Zeichenfolgenschlüsseln erstellt und die Add Methode verwendet, um einige Elemente hinzuzufügen. Das Beispiel zeigt, dass die Add Methode beim Versuch, einen doppelten Schlüssel hinzuzufügen, ausgelöst ArgumentException wird.
Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die SortedDictionary<TKey,TValue> Klasse bereitgestellt wird.
// 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
Hinweise
Jeder Schlüssel in einem SortedDictionary<TKey,TValue> muss gemäß dem Standardvergleich eindeutig sein.
SortedDictionary<TKey,TValue> erfordert eine Vergleichsimplementierung, um wichtige Vergleiche durchzuführen. Dieser Konstruktor verwendet den standardmäßigen generischen Gleichheitsvergleich Comparer<T>.Default. Wenn der Typ TKey die System.IComparable<T> generische Schnittstelle implementiert, verwendet der Standardvergleich diese Implementierung. Alternativ können Sie eine Implementierung der IComparer<T> generischen Schnittstelle mithilfe eines Konstruktors angeben, der einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
SortedDictionary<TKey,TValue>(IComparer<TKey>)
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
Initialisiert eine neue Instanz der Klasse, die SortedDictionary<TKey,TValue> leer ist, und verwendet die angegebene IComparer<T> Implementierung zum Vergleichen von Schlüsseln.
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))
Parameter
- comparer
- IComparer<TKey>
Die IComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung Comparer<T> für den Schlüsseltyp verwendet werden soll.
Beispiele
Im folgenden Codebeispiel wird ein SortedDictionary<TKey,TValue> Vergleich zwischen Groß- und Kleinschreibung für die aktuelle Kultur erstellt. Im Beispiel werden vier Elemente hinzugefügt, einige mit Kleinbuchstaben und einige mit Groß-/Kleinschreibung. Anschließend wird versucht, ein Element mit einem Schlüssel hinzuzufügen, der sich von einem vorhandenen Schlüssel nur im Fall unterscheidet, die resultierende Ausnahme abfangen und eine Fehlermeldung anzeigt. Schließlich zeigt das Beispiel die Elemente in der Sortierreihenfolge ohne Groß-/Kleinschreibung an.
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
Hinweise
Jeder Schlüssel in einem SortedDictionary<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein.
SortedDictionary<TKey,TValue> erfordert eine Vergleichsimplementierung, um wichtige Vergleiche durchzuführen. Wenn comparer dies der Fall istnull, verwendet dieser Konstruktor den standardmäßigen generischen Gleichheitsvergleich. Comparer<T>.Default Wenn der Typ TKey die System.IComparable<T> generische Schnittstelle implementiert, verwendet der Standardvergleich diese Implementierung.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
Initialisiert eine neue Instanz der SortedDictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die Standardimplementierung IComparer<T> für den Schlüsseltyp.
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))
Parameter
- dictionary
- IDictionary<TKey,TValue>
Die IDictionary<TKey,TValue> Elemente, deren Elemente in das neue SortedDictionary<TKey,TValue>kopiert werden.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie SortedDictionary<TKey,TValue> Sie eine sortierte Kopie der Informationen in einem Dictionary<TKey,TValue>, indem sie an Dictionary<TKey,TValue> den SortedDictionary<TKey,TValue>(IComparer<TKey>) Konstruktor übergeben.
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
Hinweise
Jeder Schlüssel in einem SortedDictionary<TKey,TValue> muss gemäß dem Standard-Comparer eindeutig sein. Daher muss jeder Schlüssel in der Quelle dictionary auch gemäß dem Standardvergleich eindeutig sein.
SortedDictionary<TKey,TValue> erfordert eine Vergleichsimplementierung, um wichtige Vergleiche durchzuführen. Dieser Konstruktor verwendet den standardmäßigen generischen Gleichheitsvergleich. Comparer<T>.Default Wenn der Typ TKey die System.IComparable<T> generische Schnittstelle implementiert, verwendet der Standardvergleich diese Implementierung. Alternativ können Sie eine Implementierung der IComparer<T> generischen Schnittstelle mithilfe eines Konstruktors angeben, der einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O(n Protokoll n)-Vorgang, wobei n die Anzahl der Elemente in dictionary.
Weitere Informationen
Gilt für:
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
- Quelle:
- SortedDictionary.cs
Initialisiert eine neue Instanz der SortedDictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die angegebene IComparer<T> Implementierung zum Vergleichen von Schlüsseln.
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))
Parameter
- dictionary
- IDictionary<TKey,TValue>
Die IDictionary<TKey,TValue> Elemente, deren Elemente in das neue SortedDictionary<TKey,TValue>kopiert werden.
- comparer
- IComparer<TKey>
Die IComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung Comparer<T> für den Schlüsseltyp verwendet werden soll.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie SortedDictionary<TKey,TValue> Sie mithilfe einer sortierten Kopie der Informationen in einer Groß-/Kleinschreibung Dictionary<TKey,TValue>eine sortierte Kopie der Informationen erstellen, indem sie den Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) Konstruktor übergeben. In diesem Beispiel beziehen sich die Vergleiche zwischen Groß- und Kleinschreibung auf die aktuelle Kultur.
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
Hinweise
Jeder Schlüssel in einem SortedDictionary<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein. Daher muss jeder Schlüssel in der Quelle dictionary auch gemäß dem angegebenen Vergleich eindeutig sein.
SortedDictionary<TKey,TValue> erfordert eine Vergleichsimplementierung, um wichtige Vergleiche durchzuführen. Wenn comparer dies der Fall istnull, verwendet dieser Konstruktor den standardmäßigen generischen Gleichheitsvergleich. Comparer<T>.Default Wenn der Typ TKey die System.IComparable<T> generische Schnittstelle implementiert, verwendet der Standardvergleich diese Implementierung.
Dieser Konstruktor ist ein O(n Protokoll n)-Vorgang, wobei n die Anzahl der Elemente in dictionary.