Export (0) Print
Expand All

SortedList Constructor (Int32)

Initializes a new instance of the SortedList class that is empty, has the specified 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 ( _
	initialCapacity As Integer _
)

Parameters

initialCapacity
Type: System.Int32

The initial number of elements that the SortedList object can contain.

ExceptionCondition
ArgumentOutOfRangeException

initialCapacity is less than zero.

OutOfMemoryException

There is not enough available memory to create a SortedList object with the specified initialCapacity.

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(n) operation, where n is initialCapacity.

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( 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 '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.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft