SortedList<TKey, TValue> Constructor (Int32, IComparer<TKey>)

Initializes a new instance of the SortedList<TKey, TValue> class that is empty, has the specified initial capacity, and uses the specified IComparer<T>.

Namespace:  System.Collections.Generic
Assembly:  System (in System.dll)

public SortedList(
	int capacity,
	IComparer<TKey> comparer
)

Parameters

capacity
Type: System.Int32

The initial number of elements that the SortedList<TKey, TValue> can contain.

comparer
Type: System.Collections.Generic.IComparer<TKey>

The IComparer<T> implementation to use when comparing keys.

-or-

null to use the default Comparer<T> for the type of the key.

ExceptionCondition
ArgumentOutOfRangeException

capacity is less than zero.

Every key in a SortedList<TKey, TValue> must be unique according to the specified comparer.

The capacity of a SortedList<TKey, TValue> is the number of elements that the SortedList<TKey, TValue> can hold before resizing. As elements are added to a SortedList<TKey, TValue>, the capacity is automatically increased as required by reallocating the internal array.

If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList<TKey, TValue>.

The capacity can be decreased by calling TrimExcess or by setting the Capacity property explicitly. Decreasing the capacity reallocates memory and copies all the elements in the SortedList<TKey, TValue>.

This constructor is an O(n) operation, where n is capacity.

The following code example creates a sorted list with an initial capacity of 5 and a case-insensitive comparer for the current culture. The example adds four elements, some with lower-case keys and some with upper-case keys. The example then attempts to add an element with a key that differs from an existing key only by case, catches the resulting exception, and displays an error message. Finally, the example displays the elements in case-insensitive sort order.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys, an 
        // initial capacity of 5, and a case-insensitive comparer.
        SortedList<string, string> openWith = 
                      new SortedList<string, string>(5, 
                          StringComparer.CurrentCultureIgnoreCase);

        // Add 4 elements to the list. 
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same  
        // except for case; this would be allowed with the default 
        // comparer. 
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the sorted list.");
        }

        // List the contents of the sorted list.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

BMP is already in the sorted list.

Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft