Add Method

SortedList.Add Method

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

[Visual Basic]
Public Overridable Sub Add( _
   ByVal key As Object, _
   ByVal value As Object _
) Implements IDictionary.Add
[C#]
public virtual void Add(
 object key,
 object value
);
[C++]
public: virtual void Add(
 Object* key,
 Object* value
);
[JScript]
public function Add(
   key : Object,
 value : Object
);

Parameters

key
The key of the element to add.
value
The value of the element to add. The value can be a null reference (Nothing in Visual Basic).

Implements

IDictionary.Add

Exceptions

Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).
ArgumentException An element with the specified key already exists in the SortedList.

-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.

InvalidOperationException The comparer throws an exception.

Remarks

If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled. The insertion point is determined based on the comparer selected, either explicitly or by default when the SortedList was created.

The Item property can also be used to add new elements by setting the value of a key that does not exist in the SortedList. 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 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 a null reference (Nothing in Visual Basic), but a value can be.

Example

[Visual Basic, C#, C++] The following example shows how to add elements to the SortedList.

[Visual Basic] 
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
 

[C#] 
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
*/

[C++] 

#using <mscorlib.dll>
#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 __gc *mySL = new SortedList();
   mySL->Add( S"one", S"The" );
   mySL->Add( S"two", S"quick" );
   mySL->Add( S"three", S"brown" );
   mySL->Add( S"four", S"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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortedList Class | SortedList Members | System.Collections Namespace | Item | IComparer | IComparable | Capacity

Show:
© 2016 Microsoft