Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SortedList.Add Method

Adds an element with the specified key and value to a SortedList object.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Overridable Sub Add ( _
	key As Object, _
	value As Object _
)

Parameters

key
Type: System.Object

The key of the element to add.

value
Type: System.Object

The value of the element to add. The value can be Nothing.

Implements

IDictionary.Add(Object, Object)
ExceptionCondition
ArgumentNullException

key is Nothing.

ArgumentException

An element with the specified key already exists in the SortedList object.

-or-

The SortedList is set to use the IComparable interface, and key does not implement the IComparable interface.

NotSupportedException

The SortedList is read-only.

-or-

The SortedList has a fixed size.

OutOfMemoryException

There is not enough available memory to add the element to the SortedList.

InvalidOperationException

The comparer throws an exception.

The insertion point is determined based on the comparer selected, either explicitly or by default, when the SortedList object was created.

If Count already equals Capacity, the capacity of the SortedList object is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

You can also use the Item property to add new elements by setting the value of a key that does not exist in the SortedList object (for example, myCollection["myNonexistentKey"] = myValue). However, if the specified key already exists in the SortedList, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves.

A key cannot be Nothing, but a value can be.

This method is an O(n) operation for unsorted data, where n is Count. It is an O(log n) operation if the new element is added at the end of the list. If insertion causes a resize, the operation is O(n).

The following code example shows how to add elements to a SortedList object.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesSortedList    

    Public Shared Sub Main()

        ' Creates and initializes a new SortedList. 
        Dim mySL As New SortedList()
        mySL.Add("one", "The")
        mySL.Add("two", "quick")
        mySL.Add("three", "brown")
        mySL.Add("four", "fox")

        ' Displays the SortedList.
        Console.WriteLine("The SortedList contains the following:")
        PrintKeysAndValues(mySL)
    End Sub     

    Public Shared Sub PrintKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer 
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _
               "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub 
End Class 

' This code produces the following output. 
'  
' The SortedList contains the following: 
'     -KEY-    -VALUE- 
'     four:    fox 
'     one:    The 
'     three:    brown 
'     two:    quick

.NET Framework

Supported in: 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 Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.