SortedDictionary<TKey,TValue> Konstruktorer

Definition

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

Överlagringar

Name Description
SortedDictionary<TKey,TValue>()

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som är tom och använder standardimplementeringen IComparer<T> för nyckeltypen.

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som är tom och använder den angivna IComparer<T> implementeringen för att jämföra nycklar.

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue> och använder standardimplementeringen IComparer<T> för nyckeltypen.

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue> och använder den angivna IComparer<T> implementeringen för att jämföra nycklar.

SortedDictionary<TKey,TValue>()

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som är tom och använder standardimplementeringen IComparer<T> för nyckeltypen.

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

Exempel

I följande kodexempel skapas en tom SortedDictionary<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 SortedDictionary<TKey,TValue> klassen.

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

// Add some elements to the dictionary. 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 dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted dictionary of strings, with string 
' keys. 
Dim openWith As New SortedDictionary(Of String, String)

' Add some elements to the dictionary. 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 dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

Kommentarer

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

SortedDictionary<TKey,TValue> kräver en jämförelseimplementering för att utföra viktiga jämförelser. Den här konstruktorn använder standardvärdet för allmän likhetsjämförelsen Comparer<T>.Default. Om typen TKey implementerar det System.IComparable<T> allmänna gränssnittet använder standardjäxaren den implementeringen. Du kan också ange en implementering av det IComparer<T> allmänna gränssnittet med hjälp av en konstruktor som accepterar en comparer parameter.

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

Se även

Gäller för

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som är tom och använder den angivna IComparer<T> implementeringen för att jämföra nycklar.

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

Parametrar

comparer
IComparer<TKey>

Implementeringen IComparer<T> 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 SortedDictionary<TKey,TValue> 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 SortedDictionary of strings, with string keys
        // and a case-insensitive comparer for the current culture.
        SortedDictionary<string, string> openWith =
                      new SortedDictionary<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");

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

        // List the contents of the sorted dictionary.
        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 dictionary.

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 SortedDictionary of strings, with string keys 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New SortedDictionary(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")

        ' 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 dictionary.")
        End Try
        
        ' List the contents of the sorted dictionary.
        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 dictionary.
'
'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 SortedDictionary<TKey,TValue> måste vara unik enligt den angivna jämförelsen.

SortedDictionary<TKey,TValue> kräver en jämförelseimplementering för att utföra viktiga jämförelser. Om comparer är nullanvänder den här konstruktorn standardjämförelsen för allmän likhet, Comparer<T>.Default. Om typen TKey implementerar det System.IComparable<T> allmänna gränssnittet använder standardjäxaren den implementeringen.

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

Se även

Gäller för

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue> och använder standardimplementeringen IComparer<T> för nyckeltypen.

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

Parametrar

Undantag

dictionary är null.

dictionary innehåller en eller flera dubblettnycklar.

Exempel

Följande kodexempel visar hur du använder SortedDictionary<TKey,TValue> för att skapa en sorterad kopia av informationen i en Dictionary<TKey,TValue>, genom att skicka till Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IComparer<TKey>) 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 SortedDictionary of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                  new SortedDictionary<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 SortedDictionary of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(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 SortedDictionary<TKey,TValue> måste vara unik enligt standardjäxaren. Därför måste varje nyckel i källan dictionary också vara unik enligt standardjäxaren.

SortedDictionary<TKey,TValue> kräver en jämförelseimplementering för att utföra viktiga jämförelser. Den här konstruktorn använder standardjämförelsen för allmän likhet, Comparer<T>.Default. Om typen TKey implementerar det System.IComparable<T> allmänna gränssnittet använder standardjäxaren den implementeringen. Du kan också ange en implementering av det IComparer<T> allmänna gränssnittet med hjälp av en konstruktor som accepterar en comparer parameter.

Den här konstruktorn är en O(n log n)-åtgärd, där n är antalet element i dictionary.

Se även

Gäller för

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

Initierar en ny instans av SortedDictionary<TKey,TValue> klassen som innehåller element som kopierats från den angivna IDictionary<TKey,TValue> och använder den angivna IComparer<T> implementeringen för att jämföra nycklar.

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

Parametrar

comparer
IComparer<TKey>

Implementeringen IComparer<T> 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 SortedDictionary<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 SortedDictionary<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");

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

        // Create a SortedDictionary of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                    new SortedDictionary<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 = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

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")
        
        ' List the contents of the Dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

        ' Create a SortedDictionary 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 SortedDictionary(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 = txt, Value = notepad.exe
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'
'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 SortedDictionary<TKey,TValue> måste vara unik enligt den angivna jämförelsen. Därför måste varje nyckel i källan dictionary också vara unik enligt den angivna jämförelsen.

SortedDictionary<TKey,TValue> kräver en jämförelseimplementering för att utföra viktiga jämförelser. Om comparer är nullanvänder den här konstruktorn standardjämförelsen för allmän likhet, Comparer<T>.Default. Om typen TKey implementerar det System.IComparable<T> allmänna gränssnittet använder standardjäxaren den implementeringen.

Den här konstruktorn är en O(n log n)-åtgärd, där n är antalet element i dictionary.

Se även

Gäller för