SortedList.Add(Object, Object) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Adds an element with the specified key and value to a SortedList object.
public:
virtual void Add(System::Object ^ key, System::Object ^ value);
public virtual void Add (object key, object value);
public virtual void Add (object key, object? value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Overridable Sub Add (key As Object, value As Object)
Parameters
- key
- Object
The key of the element to add.
- value
- Object
The value of the element to add. The value can be null
.
Implements
Exceptions
key
is null
.
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.
There is not enough available memory to add the element to the SortedList.
The comparer throws an exception.
Examples
The following code example shows how to add elements to a SortedList object.
#using <system.dll>
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew 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 );
}
/*
This code produces the following output.
The SortedList contains the following:
-KEY- -VALUE-
four: fox
one: The
three: brown
two: quick
*/
using System;
using System.Collections;
public class SamplesSortedList {
public static void Main() {
// Creates and initializes a new SortedList.
SortedList mySL = 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 );
}
public static void PrintKeysAndValues( SortedList myList ) {
Console.WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList.Count; i++ ) {
Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The SortedList contains the following:
-KEY- -VALUE-
four: fox
one: The
three: brown
two: quick
*/
Imports System.Collections
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
Remarks
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 null
, 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)
.