C# SortedList SortedList(IComparer, Int32)

Description

SortedList SortedList(IComparer, Int32) initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the specified IComparer interface.

Syntax

SortedList.SortedList(IComparer, Int32) has the following syntax.


public SortedList(
  IComparer comparer,
  int capacity
)

Parameters

SortedList.SortedList(IComparer, Int32) has the following parameters.

  • comparer - The IComparer implementation to use when comparing keys.
  • comparer - -or-
  • comparer - null to use the IComparable implementation of each key.
  • capacity - The initial number of elements that the SortedList object can contain.

Example

Create a SortedList using the specified CaseInsensitiveComparer, which is based on the Turkish culture (tr-TR).


using System;/*ww  w  .j  av  a 2  s  . co  m*/
using System.Collections;
using System.Globalization;

public class SamplesSortedList
{
    public static void Main()
    {
        CultureInfo myCul = new CultureInfo("tr-TR");
        SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul), 3);

        Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");

        mySL3.Add("FIRST", "Hello");
        mySL3.Add("SECOND", "World");
        mySL3.Add("THIRD", "!");
        try{
            mySL3.Add("first", "Ola!");
        }catch (ArgumentException e){
            Console.WriteLine(e);
        }
        PrintKeysAndValues(mySL3);

    }

    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