SortedList Costruttori

Definizione

Inizializza una nuova istanza della classe SortedList.

Overload

SortedList()

Inizializza una nuova istanza della classe SortedList che è vuota, ha una capacità iniziale predefinita ed è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave aggiunta all'oggetto SortedList.

SortedList(IComparer)

Inizializza una nuova istanza della classe SortedList, che è vuota, ha una capacità iniziale predefinita ed è ordinata in base all'interfaccia IComparer specificata.

SortedList(IDictionary)

Inizializza una nuova istanza della classe SortedList che contiene gli elementi copiati dal dizionario specificato, ha la capacità iniziale definita dal numero di elementi copiati ed è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave.

SortedList(Int32)

Inizializza una nuova istanza della classe SortedList che è vuota, di cui è stata specificata la capacità iniziale e che è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave aggiunta all'oggetto SortedList.

SortedList(IComparer, Int32)

Inizializza una nuova istanza della classe SortedList, che è vuota, ha una capacità iniziale specificata ed è ordinata in base all'interfaccia IComparer specificata.

SortedList(IDictionary, IComparer)

Inizializza una nuova istanza della classe SortedList che contiene gli elementi copiati dal dizionario specificato, ha la capacità iniziale definita dal numero degli elementi copiati ed è ordinata in base all'interfaccia IComparer specificata.

SortedList()

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList che è vuota, ha una capacità iniziale predefinita ed è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave aggiunta all'oggetto SortedList.

public:
 SortedList();
public SortedList ();
Public Sub New ()

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.


// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

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();
}

int main()
{
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( gcnew 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 KeyComparer.
   // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
   // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
   CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
   SortedList^ mySL3 = gcnew SortedList( gcnew 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 ComparisonType.InvariantCultureIgnoreCase value.
   SortedList^ mySL4 = gcnew 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 );

    Console::WriteLine("\n\nHit ENTER to return");
    Console::ReadLine();
}

/* 
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 : !

*/
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 : !

Commenti

Ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggetto SortedList . Gli elementi vengono ordinati in base all'implementazione IComparable di ogni chiave aggiunta a SortedList.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(1) .

Vedi anche

Si applica a

SortedList(IComparer)

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList, che è vuota, ha una capacità iniziale predefinita ed è ordinata in base all'interfaccia IComparer specificata.

public:
 SortedList(System::Collections::IComparer ^ comparer);
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)

Parametri

comparer
IComparer

Implementazione di IComparer da usare quando si confrontano le chiavi.

-oppure-

null per utilizzare l'implementazione IComparable di ciascuna chiave.

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.


// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

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();
}

int main()
{
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( gcnew 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 KeyComparer.
   // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
   // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
   CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
   SortedList^ mySL3 = gcnew SortedList( gcnew 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 ComparisonType.InvariantCultureIgnoreCase value.
   SortedList^ mySL4 = gcnew 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 );

    Console::WriteLine("\n\nHit ENTER to return");
    Console::ReadLine();
}

/* 
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 : !

*/
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 : !

Commenti

Gli elementi vengono ordinati in base all'implementazione specificata IComparer . Se il parametro è null, l'implementazione comparerIComparable di ogni chiave viene usata. Pertanto, ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggettoSortedList.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(1) .

Vedi anche

Si applica a

SortedList(IDictionary)

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList che contiene gli elementi copiati dal dizionario specificato, ha la capacità iniziale definita dal numero di elementi copiati ed è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave.

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)

Parametri

d
IDictionary

Implementazione IDictionary da copiare in un nuovo oggetto SortedList.

Eccezioni

Uno o più elementi in d non implementano l'interfaccia IComparable.

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.



using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
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();
}

int main()
{
   
   // Create the dictionary.
   Hashtable^ myHT = gcnew Hashtable;
   myHT->Add( "FIRST", "Hello" );
   myHT->Add( "SECOND", "World" );
   myHT->Add( "THIRD", "!" );
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( myHT,gcnew 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 = gcnew CultureInfo( "tr-TR" );
   SortedList^ mySL3 = gcnew SortedList( myHT, gcnew 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 ComparisonType.InvariantCultureIgnoreCase value.
   SortedList^ mySL4 = gcnew SortedList( myHT, StringComparer::InvariantCultureIgnoreCase );
   Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
   try
   {
      mySL4->Add( "first", "Ola!" );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( e );
   }

   PrintKeysAndValues( mySL4 );
}

/* 
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 : !

*/
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 : !

Commenti

Ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggetto SortedList . Gli elementi vengono ordinati in base all'implementazione IComparable di ogni chiave aggiunta a SortedList.

Un Hashtable oggetto è un esempio di IDictionary implementazione che può essere passata a questo costruttore. Il nuovo SortedList oggetto contiene una copia delle chiavi e dei valori archiviati in Hashtable.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(n) , dove n è il numero di elementi in d.

Vedi anche

Si applica a

SortedList(Int32)

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList che è vuota, di cui è stata specificata la capacità iniziale e che è ordinata in base all'interfaccia IComparable implementata da ciascuna chiave aggiunta all'oggetto SortedList.

public:
 SortedList(int initialCapacity);
public SortedList (int initialCapacity);
new System.Collections.SortedList : int -> System.Collections.SortedList
Public Sub New (initialCapacity As Integer)

Parametri

initialCapacity
Int32

Numero iniziale degli elementi che l'oggetto SortedList può contenere.

Eccezioni

initialCapacity è minore di zero.

La memoria disponibile non è sufficiente per creare un oggetto SortedList con il parametro initialCapacity specificato.

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.

// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.



using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
   Console::WriteLine( "        Capacity is {0}.", myList->Capacity );
   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();
}

int main()
{
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( gcnew 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 = gcnew CultureInfo("tr-TR");
    SortedList^ mySL3 = gcnew SortedList(gcnew 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 = gcnew 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 );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

mySL1 (default):
        Capacity is 6.
        -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()
        Capacity is 3.
        -KEY-   -VALUE-
        FIRST : Hello
        SECOND: World
        THIRD : !

mySL3 (case-insensitive comparer, Turkish culture):
        Capacity is 6.
        -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()
        Capacity is 3.
        -KEY-   -VALUE-
        FIRST : Hello
        SECOND: World
        THIRD : !

*/
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 : !

Commenti

Ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggetto SortedList . Gli elementi vengono ordinati in base all'implementazione IComparable di ogni chiave aggiunta a SortedList.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(n) , dove n è initialCapacity.

Vedi anche

Si applica a

SortedList(IComparer, Int32)

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList, che è vuota, ha una capacità iniziale specificata ed è ordinata in base all'interfaccia IComparer specificata.

public:
 SortedList(System::Collections::IComparer ^ comparer, int capacity);
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)

Parametri

comparer
IComparer

Implementazione di IComparer da usare quando si confrontano le chiavi.

-oppure-

null per utilizzare l'implementazione IComparable di ciascuna chiave.

capacity
Int32

Numero iniziale degli elementi che l'oggetto SortedList può contenere.

Eccezioni

capacity è minore di zero.

La memoria disponibile non è sufficiente per creare un oggetto SortedList con il parametro capacity specificato.

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.

// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.



using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
   Console::WriteLine( "        Capacity is {0}.", myList->Capacity );
   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();
}

int main()
{
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( gcnew 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 = gcnew CultureInfo("tr-TR");
    SortedList^ mySL3 = gcnew SortedList(gcnew 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 = gcnew 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 );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

mySL1 (default):
        Capacity is 6.
        -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()
        Capacity is 3.
        -KEY-   -VALUE-
        FIRST : Hello
        SECOND: World
        THIRD : !

mySL3 (case-insensitive comparer, Turkish culture):
        Capacity is 6.
        -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()
        Capacity is 3.
        -KEY-   -VALUE-
        FIRST : Hello
        SECOND: World
        THIRD : !

*/
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 : !

Commenti

Gli elementi vengono ordinati in base all'implementazione specificata IComparer . Se il parametro è null, l'implementazione comparerIComparable di ogni chiave viene usata. Pertanto, ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggettoSortedList.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(n) , dove n è capacity.

Vedi anche

Si applica a

SortedList(IDictionary, IComparer)

Origine:
SortedList.cs
Origine:
SortedList.cs
Origine:
SortedList.cs

Inizializza una nuova istanza della classe SortedList che contiene gli elementi copiati dal dizionario specificato, ha la capacità iniziale definita dal numero degli elementi copiati ed è ordinata in base all'interfaccia IComparer specificata.

public:
 SortedList(System::Collections::IDictionary ^ d, System::Collections::IComparer ^ comparer);
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)

Parametri

d
IDictionary

Implementazione IDictionary da copiare in un nuovo oggetto SortedList.

comparer
IComparer

Implementazione di IComparer da usare quando si confrontano le chiavi.

-oppure-

null per utilizzare l'implementazione IComparable di ciascuna chiave.

Eccezioni

comparer è null e uno o più elementi in d non implementano l'interfaccia IComparable.

Esempio

Nell'esempio di codice seguente vengono create raccolte che usano costruttori diversi SortedList e vengono illustrate le differenze nel comportamento delle raccolte.



using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
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();
}

int main()
{
   
   // Create the dictionary.
   Hashtable^ myHT = gcnew Hashtable;
   myHT->Add( "FIRST", "Hello" );
   myHT->Add( "SECOND", "World" );
   myHT->Add( "THIRD", "!" );
   
   // Create a SortedList using the default comparer.
   SortedList^ mySL1 = gcnew 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 = gcnew SortedList( myHT,gcnew 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 = gcnew CultureInfo( "tr-TR" );
   SortedList^ mySL3 = gcnew SortedList( myHT, gcnew 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 ComparisonType.InvariantCultureIgnoreCase value.
   SortedList^ mySL4 = gcnew SortedList( myHT, StringComparer::InvariantCultureIgnoreCase );
   Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
   try
   {
      mySL4->Add( "first", "Ola!" );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( e );
   }

   PrintKeysAndValues( mySL4 );
}

/* 
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 : !

*/
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 : !

Commenti

Gli elementi vengono ordinati in base all'implementazione specificata IComparer . Se il parametro è null, l'implementazione comparerIComparable di ogni chiave viene usata. Pertanto, ogni chiave deve implementare l'interfaccia IComparable per essere in grado di confrontarsi con ogni altra chiave nell'oggettoSortedList.

Un Hashtable oggetto è un esempio di IDictionary implementazione che può essere passata a questo costruttore. Il nuovo SortedList oggetto contiene una copia delle chiavi e dei valori archiviati in Hashtable.

La capacità di un SortedList oggetto è il numero di elementi che possono SortedList contenere. Man mano che gli elementi vengono aggiunti a un SortedListoggetto , la capacità viene aumentata automaticamente in base alla necessità di riallocare la matrice interna.

Se è possibile stimare le dimensioni della raccolta, specificando la capacità iniziale, la capacità iniziale elimina la necessità di eseguire una serie di operazioni di ridimensionamento durante l'aggiunta di elementi all'oggetto SortedList .

Questo costruttore è un'operazione O(n) , dove n è il numero di elementi in d.

Vedi anche

Si applica a