Namespace:
System.Collections.Generic
Assembly:
System (in System.dll)
Visual Basic (Declaration)
Public Sub New ( _
comparer As IComparer(Of TKey) _
)
Dim comparer As IComparer(Of TKey)
Dim instance As New SortedList(comparer)
public SortedList(
IComparer<TKey> comparer
)
public:
SortedList(
IComparer<TKey>^ comparer
)
public function SortedList(
comparer : IComparer<TKey>
)
Every key in a SortedList<(Of <(TKey, TValue>)>) must be unique according to the specified comparer.
This constructor uses the default value for the initial capacity of the SortedList<(Of <(TKey, TValue>)>). To set the initial capacity, use the SortedList<(Of <(TKey, TValue>)>)(Int32, IComparer<(Of <(TKey>)>)) constructor. If the final 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<(Of <(TKey, TValue>)>).
This constructor is an O(1) operation.
The following code example creates a sorted list with 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.
Imports System
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string keys and
' a case-insensitive comparer for the current culture.
Dim openWith As New SortedList(Of String, String)( _
StringComparer.CurrentCultureIgnoreCase)
' Add some 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 ex As ArgumentException
Console.WriteLine(vbLf & "BMP is already in the sorted list.")
End Try
' List the contents of the sorted list.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' 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
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys and
// a case-insensitive comparer for the current culture.
SortedList<string, string> openWith =
new SortedList<string, string>(
StringComparer.CurrentCultureIgnoreCase);
// Add some 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
*/
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
.NET Compact Framework
Supported in: 3.5, 2.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference