Export (0) Print
Expand All

SortedList Constructor

Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Sub New

Each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList object. The elements are sorted according to the IComparable implementation of each key added to the SortedList.

The capacity of a SortedList object is the number of elements that the SortedList can hold. As elements are added to a SortedList, the capacity is automatically increased as required by reallocating the internal array.

If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList object.

This constructor is an O(1) operation.

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

Imports System
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 'Main

    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 'PrintKeysAndValues

End Class 'SamplesSortedList


'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 : !

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft