C# SortedList Add
Description
SortedList Add
adds an element with the specified key
and value to a SortedList object.
Syntax
SortedList.Add
has the following syntax.
public virtual void Add(
Object key,
Object value
)
Parameters
SortedList.Add
has the following parameters.
key
- The key of the element to add.value
- The value of the element to add. The value can be null.
Returns
SortedList.Add
method returns
Example
using System;//w ww .j av a2 s .com
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
SortedList mySL4 = 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 (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("{0,-6}: {1}",myList.GetKey(i), myList.GetByIndex(i));
}
}
}
The code above generates the following result.