Aracılığıyla paylaş


SortedList<TKey,TValue> Oluşturucular

Tanım

SortedList<TKey,TValue> sınıfının yeni bir örneğini başlatır.

Aşırı Yüklemeler

Name Description
SortedList<TKey,TValue>()

Sınıfın SortedList<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve varsayılan IComparer<T>kullanır.

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

Sınıfın SortedList<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IComparer<T>öğesini kullanır.

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

Belirtilen IDictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList<TKey,TValue> başlatır, kopyalanan öğe sayısını karşılamak için yeterli kapasiteye sahiptir ve varsayılan IComparer<T>öğesini kullanır.

SortedList<TKey,TValue>(Int32)

Sınıfın SortedList<TKey,TValue> boş, belirtilen başlangıç kapasitesine sahip yeni bir örneğini başlatır ve varsayılan IComparer<T>değerini kullanır.

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

Belirtilen IDictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList<TKey,TValue> başlatır, kopyalanan öğe sayısını karşılamak için yeterli kapasiteye sahiptir ve belirtilen IComparer<T>öğesini kullanır.

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

Boş, belirtilen ilk kapasiteye SortedList<TKey,TValue> sahip olan ve belirtilen IComparer<T>öğesini kullanan sınıfın yeni bir örneğini başlatır.

SortedList<TKey,TValue>()

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Sınıfın SortedList<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve varsayılan IComparer<T>kullanır.

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

Örnekler

Aşağıdaki kod örneği, dize anahtarlarıyla boş bir SortedList<TKey,TValue> dize oluşturur ve yöntemini kullanarak Add bazı öğeler ekler. Örnek, yinelenen anahtar eklemeye çalışırken yönteminin bir ArgumentException oluşturduğunu Add gösterir.

Bu kod örneği, sınıfı için SortedList<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar varsayılan karşılaştırıcıya göre benzersiz olmalıdır.

Bu oluşturucu, ilk kapasitesi SortedList<TKey,TValue>için varsayılan değeri kullanır. İlk kapasiteyi ayarlamak için oluşturucuyu SortedList<TKey,TValue>(Int32) kullanın. Koleksiyonun son boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList<TKey,TValue>ortadan kaldırır.

Bu oluşturucu için TKeyvarsayılan karşılaştırıcıyı kullanır. Karşılaştırıcı belirtmek için oluşturucuyu SortedList<TKey,TValue>(IComparer<TKey>) kullanın. Varsayılan karşılaştırıcı Comparer<T>.Default , anahtar türünün TKey varsa bu uygulamayı uygulayıp uygulamadığını System.IComparable<T> ve kullanıp kullanmadığını denetler. Uygulanmıyorsa, Comparer<T>.Default anahtar türünün TKey uygulayıp uygulamadığını System.IComparabledenetler. Anahtar türü TKey iki arabirimi de uygulamazsa, bir parametreyi kabul eden bir System.Collections.Generic.IComparer<T> oluşturucu aşırı yüklemesinde bir comparer uygulama belirtebilirsiniz.

Bu oluşturucu bir O(1) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Sınıfın SortedList<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IComparer<T>öğesini kullanır.

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

Parametreler

comparer
IComparer<TKey>

IComparer<T> Anahtarları karşılaştırırken kullanılacak uygulama.

-veya-

null anahtarın türü için varsayılan Comparer<T> değeri kullanmak üzere.

Örnekler

Aşağıdaki kod örneği, geçerli kültür için büyük/küçük harfe duyarlı olmayan bir karşılaştırıcı ile sıralanmış bir liste oluşturur. Örnek, bazıları küçük harfli, bazıları büyük harf anahtarlı dört öğe ekler. Örnek daha sonra yalnızca büyük/küçük harfe göre mevcut anahtardan farklı bir anahtara sahip bir öğe eklemeyi dener, sonuçta elde edilen özel durumu yakalar ve bir hata iletisi görüntüler. Son olarak, örnek öğeleri büyük/küçük harfe duyarlı olmayan sıralama düzeninde görüntüler.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

Bu oluşturucu, ilk kapasitesi SortedList<TKey,TValue>için varsayılan değeri kullanır. İlk kapasiteyi ayarlamak için oluşturucuyu SortedList<TKey,TValue>(Int32, IComparer<TKey>) kullanın. Koleksiyonun son boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList<TKey,TValue>ortadan kaldırır.

Bu oluşturucu bir O(1) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Belirtilen IDictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList<TKey,TValue> başlatır, kopyalanan öğe sayısını karşılamak için yeterli kapasiteye sahiptir ve varsayılan IComparer<T>öğesini kullanır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> Öğeleri yeni SortedList<TKey,TValue>öğesine kopyalanan .

Özel durumlar

dictionary, null'e eşittir.

dictionary bir veya daha fazla yinelenen anahtar içerir.

Örnekler

Aşağıdaki kod örneği, öğesini oluşturucuya geçirerek Dictionary<TKey,TValue> içindeki bilgilerin sıralanmış bir Dictionary<TKey,TValue>kopyasını oluşturmak için SortedList<TKey,TValue>(IDictionary<TKey,TValue>) nasıl kullanılacağını SortedList<TKey,TValue> gösterir.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar varsayılan karşılaştırıcıya göre benzersiz olmalıdır; benzer şekilde, kaynaktaki dictionary her anahtar da varsayılan karşılaştırıcıya göre benzersiz olmalıdır.

Yeninin SortedList<TKey,TValue> kapasitesi içindeki dictionaryöğe sayısına ayarlanır, bu nedenle liste doldurulurken yeniden boyutlandırma gerçekleşmez.

Bu oluşturucu için TKeyvarsayılan karşılaştırıcıyı kullanır. Karşılaştırıcı belirtmek için oluşturucuyu SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) kullanın. Varsayılan karşılaştırıcı Comparer<T>.Default , anahtar türünün TKey varsa bu uygulamayı uygulayıp uygulamadığını System.IComparable<T> ve kullanıp kullanmadığını denetler. Uygulanmıyorsa, Comparer<T>.Default anahtar türünün TKey uygulayıp uygulamadığını System.IComparabledenetler. Anahtar türü TKey iki arabirimi de uygulamazsa, bir parametreyi kabul eden bir System.Collections.Generic.IComparer<T> oluşturucu aşırı yüklemesinde bir comparer uygulama belirtebilirsiniz.

içindeki dictionary anahtarlar yeniye SortedList<TKey,TValue> kopyalanır ve bir kez sıralanır ve bu da bu oluşturucuyu bir O(n günlük n) işlemi yapar.

Ayrıca bkz.

Şunlara uygulanır

SortedList<TKey,TValue>(Int32)

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Sınıfın SortedList<TKey,TValue> boş, belirtilen başlangıç kapasitesine sahip yeni bir örneğini başlatır ve varsayılan IComparer<T>değerini kullanır.

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)

Parametreler

capacity
Int32

öğesinin içerebileceği ilk öğe SortedList<TKey,TValue> sayısı.

Özel durumlar

capacity, sıfırdan küçüktür.

Örnekler

Aşağıdaki kod örneği, ilk kapasitesi 4 olan sıralanmış bir liste oluşturur ve bunu 4 girişle doldurur.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar varsayılan karşılaştırıcıya göre benzersiz olmalıdır.

'nin SortedList<TKey,TValue> kapasitesi, yeniden boyutlandırmadan önce öğesinin tutabileceği öğe SortedList<TKey,TValue> sayısıdır. öğesine öğeler eklendikçe SortedList<TKey,TValue>, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.

Koleksiyonun boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList<TKey,TValue>ortadan kaldırır.

Kapasite çağrılarak TrimExcess veya özelliği açıkça ayarlanarak Capacity azaltılabilir. Kapasiteyi azaltmak, belleği yeniden ayırır ve içindeki SortedList<TKey,TValue>tüm öğeleri kopyalar.

Bu oluşturucu için TKeyvarsayılan karşılaştırıcıyı kullanır. Karşılaştırıcı belirtmek için oluşturucuyu SortedList<TKey,TValue>(Int32, IComparer<TKey>) kullanın. Varsayılan karşılaştırıcı Comparer<T>.Default , anahtar türünün TKey varsa bu uygulamayı uygulayıp uygulamadığını System.IComparable<T> ve kullanıp kullanmadığını denetler. Uygulanmıyorsa, Comparer<T>.Default anahtar türünün TKey uygulayıp uygulamadığını System.IComparabledenetler. Anahtar türü TKey iki arabirimi de uygulamazsa, bir parametreyi kabul eden bir System.Collections.Generic.IComparer<T> oluşturucu aşırı yüklemesinde bir comparer uygulama belirtebilirsiniz.

Bu oluşturucu bir O(n) işlemidir; burada n olur capacity.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Belirtilen IDictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList<TKey,TValue> başlatır, kopyalanan öğe sayısını karşılamak için yeterli kapasiteye sahiptir ve belirtilen IComparer<T>öğesini kullanır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> Öğeleri yeni SortedList<TKey,TValue>öğesine kopyalanan .

comparer
IComparer<TKey>

IComparer<T> Anahtarları karşılaştırırken kullanılacak uygulama.

-veya-

null anahtarın türü için varsayılan Comparer<T> değeri kullanmak üzere.

Özel durumlar

dictionary, null'e eşittir.

dictionary bir veya daha fazla yinelenen anahtar içerir.

Örnekler

Aşağıdaki kod örneği, büyük/küçük harfe duyarlı olmayan bir içindeki bilgilerin büyük/küçük harfe Dictionary<TKey,TValue>duyarlı olmayan sıralanmış bir kopyasını oluşturmak için öğesini oluşturucuya SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) geçirerek Dictionary<TKey,TValue> nasıl kullanılacağını SortedList<TKey,TValue> gösterir. Bu örnekte, büyük/küçük harfe duyarlı olmayan karşılaştırıcılar geçerli kültüre yöneliktir.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır; benzer şekilde, kaynaktaki dictionary her anahtar da belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

Yeninin SortedList<TKey,TValue> kapasitesi içindeki dictionaryöğe sayısına ayarlanır, bu nedenle liste doldurulurken yeniden boyutlandırma gerçekleşmez.

içindeki dictionary anahtarlar yeniye SortedList<TKey,TValue> kopyalanır ve bir kez sıralanır ve bu da bu oluşturucuyu bir O(n günlük n) işlemi yapar.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs
Kaynak:
SortedList.cs

Boş, belirtilen ilk kapasiteye SortedList<TKey,TValue> sahip olan ve belirtilen IComparer<T>öğesini kullanan sınıfın yeni bir örneğini başlatır.

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

Parametreler

capacity
Int32

öğesinin içerebileceği ilk öğe SortedList<TKey,TValue> sayısı.

comparer
IComparer<TKey>

IComparer<T> Anahtarları karşılaştırırken kullanılacak uygulama.

-veya-

null anahtarın türü için varsayılan Comparer<T> değeri kullanmak üzere.

Özel durumlar

capacity, sıfırdan küçüktür.

Örnekler

Aşağıdaki kod örneği, ilk kapasitesi 5 olan sıralanmış bir liste ve geçerli kültür için büyük/küçük harfe duyarlı olmayan bir karşılaştırıcı oluşturur. Örnek, bazıları küçük harfli, bazıları büyük harf anahtarlı dört öğe ekler. Örnek daha sonra yalnızca büyük/küçük harfe göre mevcut anahtardan farklı bir anahtara sahip bir öğe eklemeyi dener, sonuçta elde edilen özel durumu yakalar ve bir hata iletisi görüntüler. Son olarak, örnek öğeleri büyük/küçük harfe duyarlı olmayan sıralama düzeninde görüntüler.

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

Açıklamalar

içindeki SortedList<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

'nin SortedList<TKey,TValue> kapasitesi, yeniden boyutlandırmadan önce öğesinin tutabileceği öğe SortedList<TKey,TValue> sayısıdır. öğesine öğeler eklendikçe SortedList<TKey,TValue>, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.

Koleksiyonun boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList<TKey,TValue>ortadan kaldırır.

Kapasite çağrılarak TrimExcess veya özelliği açıkça ayarlanarak Capacity azaltılabilir. Kapasiteyi azaltmak, belleği yeniden ayırır ve içindeki SortedList<TKey,TValue>tüm öğeleri kopyalar.

Bu oluşturucu bir O(n) işlemidir; burada n olur capacity.

Ayrıca bkz.

Şunlara uygulanır