Share via


SortedDictionary<TKey,TValue> Konstruktor

Definisi

Menginisialisasi instans baru kelas SortedDictionary<TKey,TValue>.

Overload

SortedDictionary<TKey,TValue>()

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang kosong dan menggunakan implementasi default IComparer<T> untuk jenis kunci.

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

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang kosong dan menggunakan implementasi yang ditentukan untuk membandingkan IComparer<T> kunci.

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

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan implementasi default IComparer<T> untuk jenis kunci.

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

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan implementasi yang ditentukan untuk membandingkan IComparer<T> kunci.

SortedDictionary<TKey,TValue>()

Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang kosong dan menggunakan implementasi default IComparer<T> untuk jenis kunci.

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

Contoh

Contoh kode berikut membuat string kosong SortedDictionary<TKey,TValue> dengan kunci string dan menggunakan Add metode untuk menambahkan beberapa elemen. Contoh menunjukkan bahwa Add metode melemparkan ArgumentException saat mencoba menambahkan kunci duplikat.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk SortedDictionary<TKey,TValue> kelas .

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

Keterangan

Setiap kunci dalam SortedDictionary<TKey,TValue> harus unik sesuai dengan perbandingan default.

SortedDictionary<TKey,TValue> membutuhkan implementasi perbandingan untuk melakukan perbandingan utama. Konstruktor ini menggunakan perbandingan Comparer<T>.Defaultkesetaraan generik default . Jika jenis TKey mengimplementasikan System.IComparable<T> antarmuka generik, perbandingan default menggunakan implementasi tersebut. Atau, Anda dapat menentukan implementasi IComparer<T> antarmuka generik dengan menggunakan konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang kosong dan menggunakan implementasi yang ditentukan untuk membandingkan IComparer<T> kunci.

public:
 SortedDictionary(System::Collections::Generic::IComparer<TKey> ^ comparer);
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))

Parameter

comparer
IComparer<TKey>

Implementasi IComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default Comparer<T> untuk jenis kunci.

Contoh

Contoh kode berikut membuat SortedDictionary<TKey,TValue> dengan pembanding yang tidak peka huruf besar/kecil untuk budaya saat ini. Contohnya menambahkan empat elemen, beberapa dengan kunci huruf kecil dan beberapa dengan kunci huruf besar. Contoh kemudian mencoba menambahkan elemen dengan kunci yang berbeda dari kunci yang ada hanya berdasarkan kasus, menangkap pengecualian yang dihasilkan, dan menampilkan pesan kesalahan. Terakhir, contoh menampilkan elemen dalam urutan pengurutan yang tidak peka huruf besar/kecil.

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

Keterangan

Setiap kunci dalam SortedDictionary<TKey,TValue> harus unik sesuai dengan perbandingan yang ditentukan.

SortedDictionary<TKey,TValue> membutuhkan implementasi perbandingan untuk melakukan perbandingan utama. Jika comparer adalah null, konstruktor ini menggunakan perbandingan kesetaraan generik default, Comparer<T>.Default. Jika jenis TKey mengimplementasikan System.IComparable<T> antarmuka generik, perbandingan default menggunakan implementasi tersebut.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan implementasi default IComparer<T> untuk jenis kunci.

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))

Parameter

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan SortedDictionary<TKey,TValue> untuk membuat salinan informasi yang diurutkan dalam Dictionary<TKey,TValue>, dengan meneruskan Dictionary<TKey,TValue> ke SortedDictionary<TKey,TValue>(IComparer<TKey>) konstruktor.

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

Keterangan

Setiap kunci dalam SortedDictionary<TKey,TValue> harus unik sesuai dengan perbandingan default; oleh karena itu, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan default.

SortedDictionary<TKey,TValue> membutuhkan implementasi perbandingan untuk melakukan perbandingan utama. Konstruktor ini menggunakan perbandingan kesetaraan generik default, Comparer<T>.Default. Jika jenis TKey mengimplementasikan System.IComparable<T> antarmuka generik, perbandingan default menggunakan implementasi tersebut. Atau, Anda dapat menentukan implementasi IComparer<T> antarmuka generik dengan menggunakan konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(n log n), di mana n adalah jumlah elemen di dictionary.

Lihat juga

Berlaku untuk

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

Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs
Sumber:
SortedDictionary.cs

Menginisialisasi instans SortedDictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan implementasi yang ditentukan untuk membandingkan IComparer<T> kunci.

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

Parameter

comparer
IComparer<TKey>

Implementasi IComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default Comparer<T> untuk jenis kunci.

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan SortedDictionary<TKey,TValue> untuk membuat salinan informasi yang diurutkan yang tidak peka huruf besar/kecil dalam peka huruf besar/kecil Dictionary<TKey,TValue>, dengan meneruskan Dictionary<TKey,TValue> ke SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktor. Dalam contoh ini, perbandingan yang tidak peka huruf besar/kecil adalah untuk budaya saat ini.

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

Keterangan

Setiap kunci dalam SortedDictionary<TKey,TValue> harus unik sesuai dengan perbandingan yang ditentukan; oleh karena itu, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan yang ditentukan.

SortedDictionary<TKey,TValue> membutuhkan implementasi perbandingan untuk melakukan perbandingan utama. Jika comparer adalah null, konstruktor ini menggunakan perbandingan kesetaraan generik default, Comparer<T>.Default. Jika jenis TKey mengimplementasikan System.IComparable<T> antarmuka generik, perbandingan default menggunakan implementasi tersebut.

Konstruktor ini adalah operasi O(n log n), di mana n adalah jumlah elemen di dictionary.

Lihat juga

Berlaku untuk