SortedList<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 SortedList<TKey,TValue>-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| SortedList<TKey,TValue>() |
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, verfügt über die Standardkapazität und verwendet den Standardwert IComparer<T>. |
| SortedList<TKey,TValue>(IComparer<TKey>) |
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet die angegebene IComparer<T>. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>) |
Initialisiert eine neue Instanz der SortedList<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue>kopiert wurden, verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente aufzunehmen, und verwendet die Standardeinstellung IComparer<T>. |
| SortedList<TKey,TValue>(Int32) |
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und die Standardeinstellung IComparer<T>verwendet. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
Initialisiert eine neue Instanz der SortedList<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen IDictionary<TKey,TValue>Element kopiert wurden, verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente aufzunehmen, und verwendet die angegebene IComparer<T>. |
| SortedList<TKey,TValue>(Int32, IComparer<TKey>) |
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und verwendet die angegebene IComparer<T>. |
SortedList<TKey,TValue>()
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, verfügt über die Standardkapazität und verwendet den Standardwert IComparer<T>.
public:
SortedList();
public SortedList();
Public Sub New ()
Beispiele
Im folgenden Codebeispiel wird eine leere SortedList<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 SortedList<TKey,TValue> Klasse bereitgestellt wird.
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
new SortedList<string, string>();
// Add some elements to the list. 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 list.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string
' keys.
Dim openWith As New SortedList(Of String, String)
' Add some elements to the list. 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 list.
Try
openWith.Add("txt", "winword.exe")
Catch
Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()
// Add some elements to the list. 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 list.
try
openWith.Add("txt", "winword.exe");
with
| :? ArgumentException ->
printfn "An element with Key = \"txt\" already exists."
Hinweise
Jeder Schlüssel in einem SortedList<TKey,TValue> muss gemäß dem Standardvergleich eindeutig sein.
Dieser Konstruktor verwendet den Standardwert für die Anfangskapazität der SortedList<TKey,TValue>. Verwenden Sie den SortedList<TKey,TValue>(Int32) Konstruktor, um die anfängliche Kapazität festzulegen. Wenn die endgültige Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden SortedList<TKey,TValue>.
Dieser Konstruktor verwendet den Standard-Comparer für TKey. Verwenden Sie den SortedList<TKey,TValue>(IComparer<TKey>) Konstruktor, um einen Vergleich anzugeben. Der Standardvergleich Comparer<T>.Default überprüft, ob der Schlüsseltyp TKey diese Implementierung implementiert System.IComparable<T> und verwendet, falls verfügbar. Wenn dies System.IComparablenicht der Fehler ist, überprüft, Comparer<T>.Default ob der Schlüsseltyp TKey implementiert wird. Wenn der Schlüsseltyp TKey keine schnittstelle implementiert, können Sie eine System.Collections.Generic.IComparer<T> Implementierung in einer Konstruktorüberladung angeben, die einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
SortedList<TKey,TValue>(IComparer<TKey>)
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet die angegebene IComparer<T>.
public:
SortedList(System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IComparer<TKey> comparer);
public SortedList(System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'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 zum Verwenden des Standardwerts Comparer<T> für den Typ des Schlüssels.
Beispiele
Im folgenden Codebeispiel wird eine sortierte Liste mit einem Vergleich ohne Groß-/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 sorted list of strings, with string keys and
// a case-insensitive comparer for the current culture.
SortedList<string, string> openWith =
new SortedList<string, string>(
StringComparer.CurrentCultureIgnoreCase);
// Add some elements to the list.
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 sorted list.");
}
// List the contents of the sorted list.
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 sorted list.
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 sorted list of strings, with string keys and
' a case-insensitive comparer for the current culture.
Dim openWith As New SortedList(Of String, String)( _
StringComparer.CurrentCultureIgnoreCase)
' Add some elements to the list.
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 sorted list.")
End Try
' List the contents of the sorted list.
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 sorted list.
'
'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 SortedList<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein.
Dieser Konstruktor verwendet den Standardwert für die Anfangskapazität der SortedList<TKey,TValue>. Verwenden Sie den SortedList<TKey,TValue>(Int32, IComparer<TKey>) Konstruktor, um die anfängliche Kapazität festzulegen. Wenn die endgültige Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden SortedList<TKey,TValue>.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
SortedList<TKey,TValue>(IDictionary<TKey,TValue>)
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der SortedList<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue>kopiert wurden, verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente aufzunehmen, und verwendet die Standardeinstellung IComparer<T>.
public:
SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.SortedList<'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 SortedList<TKey,TValue>kopiert werden.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie SortedList<TKey,TValue> Sie eine sortierte Kopie der Informationen in einem Dictionary<TKey,TValue>, indem sie an Dictionary<TKey,TValue> den SortedList<TKey,TValue>(IDictionary<TKey,TValue>) 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 SortedList of strings with string keys,
// and initialize it with the contents of the Dictionary.
SortedList<string, string> copy =
new SortedList<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 SortedList of strings with string keys,
' and initialize it with the contents of the Dictionary.
Dim copy As New SortedList(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 SortedList<TKey,TValue> muss gemäß dem Standard-Comparer eindeutig sein. Ebenso muss jeder Schlüssel in der Quelle dictionary entsprechend dem Standard-Comparer eindeutig sein.
Die Kapazität des neuen SortedList<TKey,TValue> wird auf die Anzahl der Elemente dictionaryfestgelegt, sodass keine Größenänderung stattfindet, während die Liste aufgefüllt wird.
Dieser Konstruktor verwendet den Standard-Comparer für TKey. Verwenden Sie den SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) Konstruktor, um einen Vergleich anzugeben. Der Standardvergleich Comparer<T>.Default überprüft, ob der Schlüsseltyp TKey diese Implementierung implementiert System.IComparable<T> und verwendet, falls verfügbar. Wenn dies System.IComparablenicht der Fehler ist, überprüft, Comparer<T>.Default ob der Schlüsseltyp TKey implementiert wird. Wenn der Schlüsseltyp TKey keine schnittstelle implementiert, können Sie eine System.Collections.Generic.IComparer<T> Implementierung in einer Konstruktorüberladung angeben, die einen comparer Parameter akzeptiert.
Die eingeschriebenen dictionary Schlüssel werden einmal in das neue SortedList<TKey,TValue> und sortiert, wodurch dieser Konstruktor zu einem O(n Protokoll n)-Vorgang wird.
Weitere Informationen
Gilt für:
SortedList<TKey,TValue>(Int32)
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und die Standardeinstellung IComparer<T>verwendet.
public:
SortedList(int capacity);
public SortedList(int capacity);
new System.Collections.Generic.SortedList<'Key, 'Value> : int -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer)
Parameter
- capacity
- Int32
Die anfängliche Anzahl der Elemente, die enthalten SortedList<TKey,TValue> können.
Ausnahmen
capacity ist kleiner als 0 (null).
Beispiele
Im folgenden Codebeispiel wird eine sortierte Liste mit einer Anfangskapazität von 4 erstellt und mit vier Einträgen aufgefüllt.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys and
// an initial capacity of 4.
SortedList<string, string> openWith =
new SortedList<string, string>(4);
// Add 4 elements to the list.
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 sorted list.
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:
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 sorted list of strings, with string keys and
' an initial capacity of 4.
Dim openWith As New SortedList(Of String, String)(4)
' Add 4 elements to the list.
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 sorted list.
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:
'
'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 SortedList<TKey,TValue> muss gemäß dem Standardvergleich eindeutig sein.
Die Kapazität eines Elements SortedList<TKey,TValue> ist die Anzahl der Elemente, die vor SortedList<TKey,TValue> dem Ändern der Größe aufbewahrt werden können. Wenn Elemente zu einem SortedList<TKey,TValue>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden SortedList<TKey,TValue>.
Die Kapazität kann durch Aufrufen TrimExcess oder explizites Festlegen der Capacity Eigenschaft verringert werden. Durch verringern der Kapazität wird der Arbeitsspeicher neu zuordnen und alle Elemente in der SortedList<TKey,TValue>Datei kopiert.
Dieser Konstruktor verwendet den Standard-Comparer für TKey. Verwenden Sie den SortedList<TKey,TValue>(Int32, IComparer<TKey>) Konstruktor, um einen Vergleich anzugeben. Der Standardvergleich Comparer<T>.Default überprüft, ob der Schlüsseltyp TKey diese Implementierung implementiert System.IComparable<T> und verwendet, falls verfügbar. Wenn dies System.IComparablenicht der Fehler ist, überprüft, Comparer<T>.Default ob der Schlüsseltyp TKey implementiert wird. Wenn der Schlüsseltyp TKey keine schnittstelle implementiert, können Sie eine System.Collections.Generic.IComparer<T> Implementierung in einer Konstruktorüberladung angeben, die einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O()-Vorgang, wobei n es sich dabei um einen O(n)-Vorgang handeltcapacity.
Weitere Informationen
Gilt für:
SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der SortedList<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen IDictionary<TKey,TValue>Element kopiert wurden, verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente aufzunehmen, und verwendet die angegebene IComparer<T>.
public:
SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'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 SortedList<TKey,TValue>kopiert werden.
- comparer
- IComparer<TKey>
Die IComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll.
- oder -
null zum Verwenden des Standardwerts Comparer<T> für den Typ des Schlüssels.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie SortedList<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>SortedList<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");
// Create a SortedList of strings with string keys and a
// case-insensitive equality comparer for the current culture,
// and initialize it with the contents of the Dictionary.
SortedList<string, string> copy =
new SortedList<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 = 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")
' Create a SortedList 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 SortedList(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 = 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 SortedList<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein. Ebenso muss jeder Schlüssel in der Quelle dictionary entsprechend dem angegebenen Vergleich eindeutig sein.
Die Kapazität des neuen SortedList<TKey,TValue> wird auf die Anzahl der Elemente dictionaryfestgelegt, sodass keine Größenänderung stattfindet, während die Liste aufgefüllt wird.
Die eingeschriebenen dictionary Schlüssel werden einmal in das neue SortedList<TKey,TValue> und sortiert, wodurch dieser Konstruktor zu einem O(n Protokoll n)-Vorgang wird.
Weitere Informationen
Gilt für:
SortedList<TKey,TValue>(Int32, IComparer<TKey>)
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
- Quelle:
- SortedList.cs
Initialisiert eine neue Instanz der klasse, die SortedList<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und verwendet die angegebene IComparer<T>.
public:
SortedList(int capacity, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(int capacity, System.Collections.Generic.IComparer<TKey> comparer);
public SortedList(int capacity, System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : int * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer, comparer As IComparer(Of TKey))
Parameter
- capacity
- Int32
Die anfängliche Anzahl der Elemente, die enthalten SortedList<TKey,TValue> können.
- comparer
- IComparer<TKey>
Die IComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll.
- oder -
null zum Verwenden des Standardwerts Comparer<T> für den Typ des Schlüssels.
Ausnahmen
capacity ist kleiner als 0 (null).
Beispiele
Im folgenden Codebeispiel wird eine sortierte Liste mit einer Anfangskapazität von 5 und einem Vergleich zwischen Groß- und Kleinschreibung für die aktuelle Kultur erstellt.The following code example creates a sorted list with an initial capacity of 5 and a case-insensitive comparer for the current culture. 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 sorted list of strings, with string keys, an
// initial capacity of 5, and a case-insensitive comparer.
SortedList<string, string> openWith =
new SortedList<string, string>(5,
StringComparer.CurrentCultureIgnoreCase);
// Add 4 elements to the list.
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 sorted list.");
}
// List the contents of the sorted list.
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 sorted list.
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 sorted list of strings, with string keys, an
' initial capacity of 5, and a case-insensitive comparer.
Dim openWith As New SortedList(Of String, String)(5, _
StringComparer.CurrentCultureIgnoreCase)
' Add 4 elements to the list.
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 sorted list.")
End Try
' List the contents of the sorted list.
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 sorted list.
'
'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 SortedList<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein.
Die Kapazität eines Elements SortedList<TKey,TValue> ist die Anzahl der Elemente, die vor SortedList<TKey,TValue> dem Ändern der Größe aufbewahrt werden können. Wenn Elemente zu einem SortedList<TKey,TValue>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden SortedList<TKey,TValue>.
Die Kapazität kann durch Aufrufen TrimExcess oder explizites Festlegen der Capacity Eigenschaft verringert werden. Durch verringern der Kapazität wird der Arbeitsspeicher neu zuordnen und alle Elemente in der SortedList<TKey,TValue>Datei kopiert.
Dieser Konstruktor ist ein O()-Vorgang, wobei n es sich dabei um einen O(n)-Vorgang handeltcapacity.