SortedDictionary<TKey,TValue> Oluşturucular

Tanım

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

Aşırı Yüklemeler

SortedDictionary<TKey,TValue>()

Sınıfının boş olan yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtar türü için varsayılan IComparer<T> uygulamayı kullanır.

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

Sınıfının boş olan yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtarları karşılaştırmak için belirtilen IComparer<T> uygulamayı kullanır.

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

Belirtilen IDictionary<TKey,TValue> öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtar türü için varsayılan IComparer<T> uygulamayı kullanır.

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

Sınıfının, belirtilen IDictionary<TKey,TValue> öğesinden kopyalanan öğeleri içeren yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtarları karşılaştırmak için belirtilen IComparer<T> uygulamayı kullanır.

SortedDictionary<TKey,TValue>()

Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs

Sınıfının boş olan yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtar türü için varsayılan IComparer<T> uygulamayı kullanır.

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

Örnekler

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

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

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

Açıklamalar

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

SortedDictionary<TKey,TValue> anahtar karşılaştırmaları gerçekleştirmek için bir karşılaştırıcı uygulaması gerektirir. Bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını Comparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IComparable<T> , varsayılan karşılaştırıcı bu uygulamayı kullanır. Alternatif olarak, parametre kabul eden bir oluşturucu kullanarak genel arabirimin IComparer<T> bir comparer uygulamasını belirtebilirsiniz.

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

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs

Sınıfının boş olan yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtarları karşılaştırmak için belirtilen IComparer<T> uygulamayı kullanır.

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

Parametreler

comparer
IComparer<TKey>

IComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan Comparer<T> değerin kullanılacağı uygulama.

Ö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 bir SortedDictionary<TKey,TValue> 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 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

Açıklamalar

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

SortedDictionary<TKey,TValue> anahtar karşılaştırmaları gerçekleştirmek için bir karşılaştırıcı uygulaması gerektirir. ise comparernull, bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını Comparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IComparable<T> , varsayılan karşılaştırıcı bu uygulamayı kullanır.

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

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs

Belirtilen IDictionary<TKey,TValue> öğesinden kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtar türü için varsayılan IComparer<T> uygulamayı kullanır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> öğeleri yeni SortedDictionary<TKey,TValue>öğesine kopyalanır.

Özel durumlar

dictionary, null değeridir.

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 komutunun SortedDictionary<TKey,TValue>(IComparer<TKey>) nasıl kullanılacağını SortedDictionary<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 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

Açıklamalar

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

SortedDictionary<TKey,TValue> anahtar karşılaştırmaları gerçekleştirmek için bir karşılaştırıcı uygulaması gerektirir. Bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını Comparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IComparable<T> , varsayılan karşılaştırıcı bu uygulamayı kullanır. Alternatif olarak, parametre kabul eden bir oluşturucu kullanarak genel arabirimin IComparer<T> bir comparer uygulamasını belirtebilirsiniz.

Bu oluşturucu, içindeki öğelerin dictionarysayısı olan n bir O(n günlük n) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs
Kaynak:
SortedDictionary.cs

Sınıfının, belirtilen IDictionary<TKey,TValue> öğesinden kopyalanan öğeleri içeren yeni bir örneğini SortedDictionary<TKey,TValue> başlatır ve anahtarları karşılaştırmak için belirtilen IComparer<T> uygulamayı kullanır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> öğeleri yeni SortedDictionary<TKey,TValue>öğesine kopyalanır.

comparer
IComparer<TKey>

IComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan Comparer<T> değerin kullanılacağı uygulama.

Özel durumlar

dictionary, null değeridir.

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 bir sıralanmış kopyasını oluşturucuya geçirerek Dictionary<TKey,TValue> oluşturmak için SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) nasıl kullanılacağını SortedDictionary<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");

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

Açıklamalar

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

SortedDictionary<TKey,TValue> anahtar karşılaştırmaları gerçekleştirmek için bir karşılaştırıcı uygulaması gerektirir. ise comparernull, bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını Comparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IComparable<T> , varsayılan karşılaştırıcı bu uygulamayı kullanır.

Bu oluşturucu, içindeki öğelerin dictionarysayısı olan n bir O(n günlük n) işlemidir.

Ayrıca bkz.

Şunlara uygulanır