SortedList<TKey,TValue> Konstruktorer

Definition

Initierar en ny instans av SortedList<TKey,TValue> klassen.

Överlagringar

Name Description
SortedList<TKey,TValue>()

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har standardinitieringskapaciteten och använder standardvärdet IComparer<T>.

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

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har standardinitieringskapaciteten och använder den angivna IComparer<T>.

SortedList<TKey,TValue>(IDictionary<TKey,TValue>)

Initierar en ny instans av SortedList<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue>, har tillräcklig kapacitet för att hantera antalet kopierade element och använder standardvärdet IComparer<T>.

SortedList<TKey,TValue>(Int32)

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har den angivna initiala kapaciteten och använder standardvärdet IComparer<T>.

SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Initierar en ny instans av SortedList<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue>, har tillräcklig kapacitet för att hantera antalet kopierade element och använder den angivna IComparer<T>.

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

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har den angivna initiala kapaciteten och använder den angivna IComparer<T>.

SortedList<TKey,TValue>()

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har standardinitieringskapaciteten och använder standardvärdet IComparer<T>.

public:
 SortedList();
public SortedList();
Public Sub New ()

Exempel

I följande kodexempel skapas en tom SortedList<TKey,TValue> sträng med strängnycklar och metoden används Add för att lägga till vissa element. Exemplet visar att Add metoden genererar en ArgumentException när du försöker lägga till en dubblettnyckel.

Det här kodexemplet är en del av ett större exempel för SortedList<TKey,TValue> klassen.

// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
    new SortedList<string, string>();

// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of String, String)

' Add some elements to the list. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()

// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

// The Add method throws an exception if the new key is
// already in the list.
try
    openWith.Add("txt", "winword.exe");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt standardjäxaren.

Den här konstruktorn använder standardvärdet för den initiala kapaciteten för SortedList<TKey,TValue>. Använd konstruktorn för SortedList<TKey,TValue>(Int32) att ange den initiala kapaciteten. Om samlingens slutliga storlek kan beräknas eliminerar du behovet av att utföra ett antal storleksändringsåtgärder när du lägger till element i SortedList<TKey,TValue>.

Den här konstruktorn använder standardjäxaren för TKey. Om du vill ange en jämförelse använder du SortedList<TKey,TValue>(IComparer<TKey>) konstruktorn. Standardjäxaren Comparer<T>.Default kontrollerar om nyckeltypen TKey implementerar System.IComparable<T> och använder den implementeringen, om den är tillgänglig. Comparer<T>.Default Annars kontrollerar du om nyckeltypen TKey implementerar System.IComparable. Om nyckeltypen TKey inte implementerar något av gränssnitten kan du ange en System.Collections.Generic.IComparer<T> implementering i en konstruktoröverlagring som accepterar en comparer parameter.

Den här konstruktorn är en O(1)-åtgärd.

Se även

Gäller för

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

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har standardinitieringskapaciteten och använder den angivna IComparer<T>.

public:
 SortedList(System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (comparer As IComparer(Of TKey))

Parametrar

comparer
IComparer<TKey>

Den IComparer<T> implementering som ska användas vid jämförelse av nycklar.

-eller-

null för att använda standardvärdet Comparer<T> för typen av nyckel.

Exempel

I följande kodexempel skapas en sorterad lista med en skiftlägeskänslig jämförelse för den aktuella kulturen. Exemplet lägger till fyra element, vissa med gemener och några med versaler. Exemplet försöker sedan lägga till ett element med en nyckel som endast skiljer sig från en befintlig nyckel från fall till fall, fångar det resulterande undantaget och visar ett felmeddelande. Slutligen visar exemplet elementen i skiftlägesokänslig sorteringsordning.

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
 */
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

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt den angivna jämförelsen.

Den här konstruktorn använder standardvärdet för den initiala kapaciteten för SortedList<TKey,TValue>. Använd konstruktorn för SortedList<TKey,TValue>(Int32, IComparer<TKey>) att ange den initiala kapaciteten. Om samlingens slutliga storlek kan beräknas eliminerar du behovet av att utföra ett antal storleksändringsåtgärder när du lägger till element i SortedList<TKey,TValue>.

Den här konstruktorn är en O(1)-åtgärd.

Se även

Gäller för

SortedList<TKey,TValue>(IDictionary<TKey,TValue>)

Initierar en ny instans av SortedList<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue>, har tillräcklig kapacitet för att hantera antalet kopierade element och använder standardvärdet IComparer<T>.

public:
 SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue))

Parametrar

dictionary
IDictionary<TKey,TValue>

Vars IDictionary<TKey,TValue> element kopieras till den nya SortedList<TKey,TValue>.

Undantag

dictionary är null.

dictionary innehåller en eller flera dubblettnycklar.

Exempel

Följande kodexempel visar hur du använder SortedList<TKey,TValue> för att skapa en sorterad kopia av informationen i en Dictionary<TKey,TValue>, genom att skicka till Dictionary<TKey,TValue>SortedList<TKey,TValue>(IDictionary<TKey,TValue>) konstruktorn.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith =
                                  new Dictionary<string, string>();

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Create a SortedList of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
                  new SortedList<string, string>(openWith);

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

/* This code example produces the following output:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string 
        ' keys.
        Dim openWith As New Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a SortedList of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedList(Of String, String)(openWith)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt standardjäxaren. På samma sätt måste varje nyckel i källan dictionary också vara unik enligt standardjäxaren.

Kapaciteten för den nya SortedList<TKey,TValue> är inställd på antalet element i dictionary, så ingen storleksändring sker när listan fylls i.

Den här konstruktorn använder standardjäxaren för TKey. Om du vill ange en jämförelse använder du SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktorn. Standardjäxaren Comparer<T>.Default kontrollerar om nyckeltypen TKey implementerar System.IComparable<T> och använder den implementeringen, om den är tillgänglig. Comparer<T>.Default Annars kontrollerar du om nyckeltypen TKey implementerar System.IComparable. Om nyckeltypen TKey inte implementerar något av gränssnitten kan du ange en System.Collections.Generic.IComparer<T> implementering i en konstruktoröverlagring som accepterar en comparer parameter.

Nycklarna i dictionary kopieras till den nya SortedList<TKey,TValue> och sorterade en gång, vilket gör konstruktorn till en O(n log n)-åtgärd.

Se även

Gäller för

SortedList<TKey,TValue>(Int32)

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har den angivna initiala kapaciteten och använder standardvärdet IComparer<T>.

public:
 SortedList(int capacity);
public SortedList(int capacity);
new System.Collections.Generic.SortedList<'Key, 'Value> : int -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer)

Parametrar

capacity
Int32

Det inledande antalet element som SortedList<TKey,TValue> kan innehålla.

Undantag

capacity är mindre än noll.

Exempel

I följande kodexempel skapas en sorterad lista med en initial kapacitet på 4 och fyller den med 4 poster.

using System;
using System.Collections.Generic;

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

        // 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");

        // 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:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted list of strings, with string keys and
        ' an initial capacity of 4.
        Dim openWith As New SortedList(Of String, String)(4)
        
        ' 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")
        
        ' 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:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt standardjäxaren.

Kapaciteten för en SortedList<TKey,TValue> är antalet element som SortedList<TKey,TValue> kan behållas innan storleksändringen. När element läggs till i en SortedList<TKey,TValue>ökas kapaciteten automatiskt efter behov genom att den interna matrisen omplaceras.

Om storleken på samlingen kan uppskattas eliminerar du behovet av att utföra ett antal storleksändringsåtgärder när du lägger till element i SortedList<TKey,TValue>.

Kapaciteten kan minskas genom att anropa TrimExcess eller genom att uttryckligen ange egenskapen Capacity . Genom att minska kapaciteten omallokerar minnet och kopieras alla element i SortedList<TKey,TValue>.

Den här konstruktorn använder standardjäxaren för TKey. Om du vill ange en jämförelse använder du SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktorn. Standardjäxaren Comparer<T>.Default kontrollerar om nyckeltypen TKey implementerar System.IComparable<T> och använder den implementeringen, om den är tillgänglig. Comparer<T>.Default Annars kontrollerar du om nyckeltypen TKey implementerar System.IComparable. Om nyckeltypen TKey inte implementerar något av gränssnitten kan du ange en System.Collections.Generic.IComparer<T> implementering i en konstruktoröverlagring som accepterar en comparer parameter.

Den här konstruktorn är en O(n)-åtgärd, där n är capacity.

Se även

Gäller för

SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Initierar en ny instans av SortedList<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue>, har tillräcklig kapacitet för att hantera antalet kopierade element och använder den angivna IComparer<T>.

public:
 SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue), comparer As IComparer(Of TKey))

Parametrar

dictionary
IDictionary<TKey,TValue>

Vars IDictionary<TKey,TValue> element kopieras till den nya SortedList<TKey,TValue>.

comparer
IComparer<TKey>

Den IComparer<T> implementering som ska användas vid jämförelse av nycklar.

-eller-

null för att använda standardvärdet Comparer<T> för typen av nyckel.

Undantag

dictionary är null.

dictionary innehåller en eller flera dubblettnycklar.

Exempel

Följande kodexempel visar hur du använder SortedList<TKey,TValue> för att skapa en skiftlägeskänslig sorterad kopia av informationen i ett skiftlägesokänsligt Dictionary<TKey,TValue>, genom att skicka Dictionary<TKey,TValue> till SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktorn. I det här exemplet är skiftlägesokänsliga jämförelser för den aktuella kulturen.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys and
        // a case-insensitive equality comparer for the current
        // culture.
        Dictionary<string, string> openWith =
            new Dictionary<string, string>
                (StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("Bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Create a SortedList of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
            new SortedList<string, string>(openWith,
                StringComparer.CurrentCultureIgnoreCase);

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

/* This code example produces the following output:

Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys and
        ' a case-insensitive equality comparer for the current 
        ' culture.
        Dim openWith As New Dictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a SortedList of strings with string keys and a 
        ' case-insensitive equality comparer for the current culture,
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedList(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt den angivna jämförelsen. På samma sätt måste varje nyckel i källan dictionary också vara unik enligt den angivna jämförelsen.

Kapaciteten för den nya SortedList<TKey,TValue> är inställd på antalet element i dictionary, så ingen storleksändring sker när listan fylls i.

Nycklarna i dictionary kopieras till den nya SortedList<TKey,TValue> och sorterade en gång, vilket gör konstruktorn till en O(n log n)-åtgärd.

Se även

Gäller för

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

Initierar en ny instans av SortedList<TKey,TValue> klassen som är tom, har den angivna initiala kapaciteten och använder den angivna IComparer<T>.

public:
 SortedList(int capacity, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(int capacity, System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : int * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer, comparer As IComparer(Of TKey))

Parametrar

capacity
Int32

Det inledande antalet element som SortedList<TKey,TValue> kan innehålla.

comparer
IComparer<TKey>

Den IComparer<T> implementering som ska användas vid jämförelse av nycklar.

-eller-

null för att använda standardvärdet Comparer<T> för typen av nyckel.

Undantag

capacity är mindre än noll.

Exempel

I följande kodexempel skapas en sorterad lista med en initial kapacitet på 5 och en skiftlägeskänslig jämförelse för den aktuella kulturen. Exemplet lägger till fyra element, vissa med gemener och några med versaler. Exemplet försöker sedan lägga till ett element med en nyckel som endast skiljer sig från en befintlig nyckel från fall till fall, fångar det resulterande undantaget och visar ett felmeddelande. Slutligen visar exemplet elementen i skiftlägesokänslig sorteringsordning.

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
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted list of strings, with string keys, an
        ' initial capacity of 5, and a case-insensitive comparer.
        Dim openWith As New SortedList(Of 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 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

Kommentarer

Varje nyckel i en SortedList<TKey,TValue> måste vara unik enligt den angivna jämförelsen.

Kapaciteten för en SortedList<TKey,TValue> är antalet element som SortedList<TKey,TValue> kan behållas innan storleksändringen. När element läggs till i en SortedList<TKey,TValue>ökas kapaciteten automatiskt efter behov genom att den interna matrisen omplaceras.

Om storleken på samlingen kan uppskattas eliminerar du behovet av att utföra ett antal storleksändringsåtgärder när du lägger till element i SortedList<TKey,TValue>.

Kapaciteten kan minskas genom att anropa TrimExcess eller genom att uttryckligen ange egenskapen Capacity . Genom att minska kapaciteten omallokerar minnet och kopieras alla element i SortedList<TKey,TValue>.

Den här konstruktorn är en O(n)-åtgärd, där n är capacity.

Se även

Gäller för