C# SortedList SortedList(Int32)

Description

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

Syntax

SortedList.SortedList(Int32) has the following syntax.


public SortedList(
  int initialCapacity
)

Parameters

SortedList.SortedList(Int32) has the following parameters.

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

Example


using System;/*w  w  w  .j a va  2 s  . c  o  m*/
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {

        SortedList mySL1 = new SortedList( 3 );
        Console.WriteLine("mySL1 (default):");
        mySL1.Add("FIRST", "Hello");
        mySL1.Add("SECOND", "World");
        mySL1.Add("THIRD", "!");
        try
        {
            mySL1.Add("first", "Ola!");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e);
        }
        PrintKeysAndValues(mySL1);

    }

    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.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack