SortedList 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-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| SortedList() |
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, hat die Standard-Anfangskapazität und wird nach der Schnittstelle sortiert, die IComparable von jedem Schlüssel implementiert wird, der das SortedList Objekt hinzugefügt hat. |
| SortedList(IComparer) |
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, verfügt über die Standard-Anfangskapazität und wird entsprechend der angegebenen IComparer Schnittstelle sortiert. |
| SortedList(IDictionary) |
Initialisiert eine neue Instanz der SortedList Klasse, die Elemente enthält, die aus dem angegebenen Wörterbuch kopiert wurden, hat die gleiche Anfangskapazität wie die Anzahl der kopierten Elemente und wird nach der Schnittstelle sortiert, die IComparable von jedem Schlüssel implementiert wird. |
| SortedList(Int32) |
Initialisiert eine neue Instanz der Klasse, die SortedList leer ist, die angegebene Anfangskapazität hat und nach der Schnittstelle sortiert wird, die IComparable von jedem Schlüssel implementiert wird, der dem SortedList Objekt hinzugefügt wurde. |
| SortedList(IComparer, Int32) |
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, die angegebene Anfangskapazität hat und nach der angegebenen IComparer Schnittstelle sortiert wird. |
| SortedList(IDictionary, IComparer) |
Initialisiert eine neue Instanz der SortedList Klasse, die Elemente enthält, die aus dem angegebenen Wörterbuch kopiert wurden, hat die gleiche Anfangskapazität wie die Anzahl der kopierten Elemente und wird entsprechend der angegebenen IComparer Schnittstelle sortiert. |
SortedList()
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, hat die Standard-Anfangskapazität und wird nach der Schnittstelle sortiert, die IComparable von jedem Schlüssel implementiert wird, der das SortedList Objekt hinzugefügt hat.
public:
SortedList();
public SortedList();
Public Sub New ()
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList();
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul));
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList()
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Jeder Schlüssel muss die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt durchführen zu können. Die Elemente werden nach der IComparable Implementierung jedes Schlüssels sortiert, der der SortedList.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein O(1) Vorgang.
Weitere Informationen
Gilt für:
SortedList(IComparer)
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, verfügt über die Standard-Anfangskapazität und wird entsprechend der angegebenen IComparer Schnittstelle sortiert.
public:
SortedList(System::Collections::IComparer ^ comparer);
public SortedList(System.Collections.IComparer comparer);
new System.Collections.SortedList : System.Collections.IComparer -> System.Collections.SortedList
Public Sub New (comparer As IComparer)
Parameter
- comparer
- IComparer
Die IComparer Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll.
-oder-
null die Implementierung der einzelnen Schlüssel zu verwenden IComparable .
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList();
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul));
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList()
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Die Elemente werden nach der angegebenen IComparer Implementierung sortiert. Wenn der comparer Parameter lautet null, wird die IComparable Implementierung jedes Schlüssels verwendet. Daher muss jeder Schlüssel die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt zu erhalten.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein O(1) Vorgang.
Weitere Informationen
- IComparer
- IComparable
- Capacity
- Ausführen von Culture-Insensitive Zeichenfolgenvorgängen in Auflistungen
Gilt für:
SortedList(IDictionary)
Initialisiert eine neue Instanz der SortedList Klasse, die Elemente enthält, die aus dem angegebenen Wörterbuch kopiert wurden, hat die gleiche Anfangskapazität wie die Anzahl der kopierten Elemente und wird nach der Schnittstelle sortiert, die IComparable von jedem Schlüssel implementiert wird.
public:
SortedList(System::Collections::IDictionary ^ d);
public SortedList(System.Collections.IDictionary d);
new System.Collections.SortedList : System.Collections.IDictionary -> System.Collections.SortedList
Public Sub New (d As IDictionary)
Parameter
Die IDictionary Implementierung, die in ein neues SortedList Objekt kopiert werden soll.
Ausnahmen
d ist null.
Mindestens ein Element in d der IComparable Schnittstelle wird nicht implementiert.
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create the dictionary.
Hashtable myHT = new Hashtable();
myHT.Add("FIRST", "Hello");
myHT.Add("SECOND", "World");
myHT.Add("THIRD", "!");
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList(myHT);
Console.WriteLine("mySL1 (default):");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul));
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
myHT, StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create the dictionary.
Dim myHT As New Hashtable()
myHT.Add("FIRST", "Hello")
myHT.Add("SECOND", "World")
myHT.Add("THIRD", "!")
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList(myHT)
Console.WriteLine("mySL1 (default):")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Jeder Schlüssel muss die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt durchführen zu können. Die Elemente werden nach der IComparable Implementierung jedes Schlüssels sortiert, der der SortedList.
Ein Hashtable Objekt ist ein Beispiel für eine IDictionary Implementierung, die an diesen Konstruktor übergeben werden kann. Das neue SortedList Objekt enthält eine Kopie der Schlüssel und Werte, die in der HashtableDatei gespeichert sind.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein O(n) Vorgang, wobei n die Anzahl der Elemente in d.
Weitere Informationen
- IDictionary
- IComparable
- Hashtable
- Capacity
- Ausführen von Culture-Insensitive Zeichenfolgenvorgängen in Auflistungen
Gilt für:
SortedList(Int32)
Initialisiert eine neue Instanz der Klasse, die SortedList leer ist, die angegebene Anfangskapazität hat und nach der Schnittstelle sortiert wird, die IComparable von jedem Schlüssel implementiert wird, der dem SortedList Objekt hinzugefügt wurde.
public:
SortedList(int initialCapacity);
public SortedList(int initialCapacity);
new System.Collections.SortedList : int -> System.Collections.SortedList
Public Sub New (initialCapacity As Integer)
Parameter
- initialCapacity
- Int32
Die anfängliche Anzahl von Elementen, die das SortedList Objekt enthalten kann.
Ausnahmen
initialCapacity ist kleiner als 0 (null).
Es steht nicht genügend Arbeitsspeicher zur Verfügung, um ein SortedList Objekt mit dem angegebenen initialCapacityObjekt zu erstellen.
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList( 3 );
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3);
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 =
new SortedList(new CaseInsensitiveComparer(myCul), 3);
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase, 3);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList( 3 )
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3)
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3)
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase, 3)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Jeder Schlüssel muss die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt durchführen zu können. Die Elemente werden nach der IComparable Implementierung jedes Schlüssels sortiert, der der SortedList.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein Vorgang, bei dem O(n) es sich um einen n Vorgang handeltinitialCapacity.
Weitere Informationen
Gilt für:
SortedList(IComparer, Int32)
Initialisiert eine neue Instanz der klasse, die SortedList leer ist, die angegebene Anfangskapazität hat und nach der angegebenen IComparer Schnittstelle sortiert wird.
public:
SortedList(System::Collections::IComparer ^ comparer, int capacity);
public SortedList(System.Collections.IComparer comparer, int capacity);
new System.Collections.SortedList : System.Collections.IComparer * int -> System.Collections.SortedList
Public Sub New (comparer As IComparer, capacity As Integer)
Parameter
- comparer
- IComparer
Die IComparer Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll.
-oder-
null die Implementierung der einzelnen Schlüssel zu verwenden IComparable .
- capacity
- Int32
Die anfängliche Anzahl von Elementen, die das SortedList Objekt enthalten kann.
Ausnahmen
capacity ist kleiner als 0 (null).
Es steht nicht genügend Arbeitsspeicher zur Verfügung, um ein SortedList Objekt mit dem angegebenen capacityObjekt zu erstellen.
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList( 3 );
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3);
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 =
new SortedList(new CaseInsensitiveComparer(myCul), 3);
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase, 3);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList( 3 )
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3)
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3)
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase, 3)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Die Elemente werden nach der angegebenen IComparer Implementierung sortiert. Wenn der comparer Parameter lautet null, wird die IComparable Implementierung jedes Schlüssels verwendet. Daher muss jeder Schlüssel die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt zu erhalten.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein Vorgang, bei dem O(n) es sich um einen n Vorgang handeltcapacity.
Weitere Informationen
- IComparer
- IComparable
- Capacity
- Ausführen von Culture-Insensitive Zeichenfolgenvorgängen in Auflistungen
Gilt für:
SortedList(IDictionary, IComparer)
Initialisiert eine neue Instanz der SortedList Klasse, die Elemente enthält, die aus dem angegebenen Wörterbuch kopiert wurden, hat die gleiche Anfangskapazität wie die Anzahl der kopierten Elemente und wird entsprechend der angegebenen IComparer Schnittstelle sortiert.
public:
SortedList(System::Collections::IDictionary ^ d, System::Collections::IComparer ^ comparer);
public SortedList(System.Collections.IDictionary d, System.Collections.IComparer comparer);
new System.Collections.SortedList : System.Collections.IDictionary * System.Collections.IComparer -> System.Collections.SortedList
Public Sub New (d As IDictionary, comparer As IComparer)
Parameter
Die IDictionary Implementierung, die in ein neues SortedList Objekt kopiert werden soll.
- comparer
- IComparer
Die IComparer Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll.
-oder-
null die Implementierung der einzelnen Schlüssel zu verwenden IComparable .
Ausnahmen
d ist null.
comparer ist null, und mindestens ein Element in d der IComparable Schnittstelle wird nicht implementiert.
Beispiele
Im folgenden Codebeispiel werden Sammlungen mit unterschiedlichen SortedList Konstruktoren erstellt und die Unterschiede im Verhalten der Auflistungen veranschaulicht.
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create the dictionary.
Hashtable myHT = new Hashtable();
myHT.Add("FIRST", "Hello");
myHT.Add("SECOND", "World");
myHT.Add("THIRD", "!");
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList(myHT);
Console.WriteLine("mySL1 (default):");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul));
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
myHT, StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create the dictionary.
Dim myHT As New Hashtable()
myHT.Add("FIRST", "Hello")
myHT.Add("SECOND", "World")
myHT.Add("THIRD", "!")
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList(myHT)
Console.WriteLine("mySL1 (default):")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Hinweise
Die Elemente werden nach der angegebenen IComparer Implementierung sortiert. Wenn der comparer Parameter lautet null, wird die IComparable Implementierung jedes Schlüssels verwendet. Daher muss jeder Schlüssel die IComparable Schnittstelle implementieren, um Vergleiche mit jedem anderen Schlüssel im SortedList Objekt zu erhalten.
Ein Hashtable Objekt ist ein Beispiel für eine IDictionary Implementierung, die an diesen Konstruktor übergeben werden kann. Das neue SortedList Objekt enthält eine Kopie der Schlüssel und Werte, die in der HashtableDatei gespeichert sind.
Die Kapazität eines SortedList Objekts ist die Anzahl der Elemente, die aufbewahrt SortedList werden können. Wenn Elemente zu einem SortedListElement 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, beim Hinzufügen von Elementen zum SortedList Objekt eine Reihe von Größenänderungsvorgängen auszuführen.
Dieser Konstruktor ist ein O(n) Vorgang, wobei n die Anzahl der Elemente in d.
Weitere Informationen
- IDictionary
- IComparer
- IComparable
- Hashtable
- Capacity
- Ausführen von Culture-Insensitive Zeichenfolgenvorgängen in Auflistungen