Bagikan melalui


SortedList<TKey,TValue> Konstruktor

Definisi

Menginisialisasi instans baru kelas SortedList<TKey,TValue>.

Overload

SortedList<TKey,TValue>()

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan default IComparer<T>.

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

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan yang ditentukan IComparer<T>.

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

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue>, memiliki kapasitas yang memadai untuk mengakomodasi jumlah elemen yang disalin, dan menggunakan default IComparer<T>.

SortedList<TKey,TValue>(Int32)

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan default IComparer<T>.

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

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue>, memiliki kapasitas yang memadai untuk mengakomodasi jumlah elemen yang disalin, dan menggunakan yang ditentukan IComparer<T>.

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

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan yang ditentukan IComparer<T>.

SortedList<TKey,TValue>()

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan default IComparer<T>.

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

Contoh

Contoh kode berikut membuat string kosong SortedList<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 SortedList<TKey,TValue> kelas .

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew 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.
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."

Keterangan

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

Konstruktor ini menggunakan nilai default untuk kapasitas SortedList<TKey,TValue>awal . Untuk mengatur kapasitas awal, gunakan SortedList<TKey,TValue>(Int32) konstruktor. Jika ukuran akhir koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke SortedList<TKey,TValue>.

Konstruktor ini menggunakan perbandingan default untuk TKey. Untuk menentukan perbandingan, gunakan SortedList<TKey,TValue>(IComparer<TKey>) konstruktor. Pembanding Comparer<T>.Default default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable<T> dan menggunakan implementasi tersebut, jika tersedia. Jika tidak, Comparer<T>.Default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable. Jika jenis TKey kunci tidak mengimplementasikan salah satu antarmuka, Anda dapat menentukan System.Collections.Generic.IComparer<T> implementasi dalam kelebihan beban konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan yang ditentukan IComparer<T>.

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

Parameter

comparer
IComparer<TKey>

Implementasi yang IComparer<T> digunakan saat membandingkan kunci.

-atau-

null untuk menggunakan default Comparer<T> untuk jenis kunci.

Contoh

Contoh kode berikut membuat daftar yang diurutkan dengan perbandingan 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 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

Keterangan

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

Konstruktor ini menggunakan nilai default untuk kapasitas SortedList<TKey,TValue>awal . Untuk mengatur kapasitas awal, gunakan SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktor. Jika ukuran akhir koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke SortedList<TKey,TValue>.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue>, memiliki kapasitas yang memadai untuk mengakomodasi jumlah elemen yang disalin, dan menggunakan default 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))

Parameter

dictionary
IDictionary<TKey,TValue>

Elemen IDictionary<TKey,TValue> yang disalin ke yang baru SortedList<TKey,TValue>.

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan SortedList<TKey,TValue> untuk membuat salinan informasi yang diurutkan dalam Dictionary<TKey,TValue>, dengan meneruskan Dictionary<TKey,TValue> ke SortedList<TKey,TValue>(IDictionary<TKey,TValue>) 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 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

Keterangan

Setiap kunci dalam SortedList<TKey,TValue> harus unik sesuai dengan perbandingan default; demikian juga, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan default.

Kapasitas baru SortedList<TKey,TValue> diatur ke jumlah elemen di dictionary, sehingga tidak ada perubahan ukuran yang terjadi saat daftar sedang diisi.

Konstruktor ini menggunakan perbandingan default untuk TKey. Untuk menentukan perbandingan, gunakan SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktor. Pembanding Comparer<T>.Default default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable<T> dan menggunakan implementasi tersebut, jika tersedia. Jika tidak, Comparer<T>.Default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable. Jika jenis TKey kunci tidak mengimplementasikan salah satu antarmuka, Anda dapat menentukan System.Collections.Generic.IComparer<T> implementasi dalam kelebihan beban konstruktor yang menerima comparer parameter.

Kunci di disalin ke yang baru dan diurutkan dictionary sekali, yang menjadikan konstruktor ini sebagai operasi O(n log n).SortedList<TKey,TValue>

Lihat juga

Berlaku untuk

SortedList<TKey,TValue>(Int32)

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan default 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)

Parameter

capacity
Int32

Jumlah awal elemen yang dapat dikandung SortedList<TKey,TValue> .

Pengecualian

capacity kurang dari nol.

Contoh

Contoh kode berikut membuat daftar yang diurutkan dengan kapasitas awal 4 dan mengisinya dengan 4 entri.

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

Keterangan

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

Kapasitas adalah SortedList<TKey,TValue> jumlah elemen yang dapat ditahan SortedList<TKey,TValue> sebelum mengubah ukuran. Saat elemen ditambahkan ke SortedList<TKey,TValue>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.

Jika ukuran koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke SortedList<TKey,TValue>.

Kapasitas dapat dikurangi dengan memanggil TrimExcess atau dengan mengatur Capacity properti secara eksplisit. Mengurangi kapasitas mengalokasikan memori dan menyalin semua elemen di SortedList<TKey,TValue>.

Konstruktor ini menggunakan perbandingan default untuk TKey. Untuk menentukan perbandingan, gunakan SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktor. Pembanding Comparer<T>.Default default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable<T> dan menggunakan implementasi tersebut, jika tersedia. Jika tidak, Comparer<T>.Default memeriksa apakah jenis TKey kunci mengimplementasikan System.IComparable. Jika jenis TKey kunci tidak mengimplementasikan salah satu antarmuka, Anda dapat menentukan System.Collections.Generic.IComparer<T> implementasi dalam kelebihan beban konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(n), di mana n adalah capacity.

Lihat juga

Berlaku untuk

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

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue>, memiliki kapasitas yang memadai untuk mengakomodasi jumlah elemen yang disalin, dan menggunakan yang ditentukan 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);
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))

Parameter

dictionary
IDictionary<TKey,TValue>

Elemen IDictionary<TKey,TValue> yang disalin ke yang baru SortedList<TKey,TValue>.

comparer
IComparer<TKey>

Implementasi yang IComparer<T> digunakan saat membandingkan kunci.

-atau-

null untuk menggunakan default Comparer<T> untuk jenis kunci.

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan SortedList<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 SortedList<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");

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

Keterangan

Setiap kunci dalam SortedList<TKey,TValue> harus unik sesuai dengan perbandingan yang ditentukan; demikian juga, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan yang ditentukan.

Kapasitas baru SortedList<TKey,TValue> diatur ke jumlah elemen di dictionary, sehingga tidak ada perubahan ukuran yang terjadi saat daftar sedang diisi.

Kunci di disalin ke yang baru dan diurutkan dictionary sekali, yang menjadikan konstruktor ini sebagai operasi O(n log n).SortedList<TKey,TValue>

Lihat juga

Berlaku untuk

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

Sumber:
SortedList.cs
Sumber:
SortedList.cs
Sumber:
SortedList.cs

Menginisialisasi instans SortedList<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan yang ditentukan IComparer<T>.

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

Parameter

capacity
Int32

Jumlah awal elemen yang dapat dikandung SortedList<TKey,TValue> .

comparer
IComparer<TKey>

Implementasi yang IComparer<T> digunakan saat membandingkan kunci.

-atau-

null untuk menggunakan default Comparer<T> untuk jenis kunci.

Pengecualian

capacity kurang dari nol.

Contoh

Contoh kode berikut membuat daftar yang diurutkan dengan kapasitas awal 5 dan 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 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

Keterangan

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

Kapasitas adalah SortedList<TKey,TValue> jumlah elemen yang dapat ditahan SortedList<TKey,TValue> sebelum mengubah ukuran. Saat elemen ditambahkan ke SortedList<TKey,TValue>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.

Jika ukuran koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke SortedList<TKey,TValue>.

Kapasitas dapat dikurangi dengan memanggil TrimExcess atau dengan mengatur Capacity properti secara eksplisit. Mengurangi kapasitas mengalokasikan memori dan menyalin semua elemen di SortedList<TKey,TValue>.

Konstruktor ini adalah operasi O(n), di mana n adalah capacity.

Lihat juga

Berlaku untuk