SortedDictionary<TKey,TValue> コンストラクター

定義

SortedDictionary<TKey,TValue> クラスの新しいインスタンスを初期化します。

オーバーロード

SortedDictionary<TKey,TValue>()

空で、キーの型の既定の SortedDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

空で、指定した SortedDictionary<TKey,TValue> を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、キーの型の既定の IDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、指定した IDictionary<TKey,TValue> 実装を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。

SortedDictionary<TKey,TValue>()

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

空で、キーの型の既定の SortedDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

次のコード例では、文字列キーを SortedDictionary<TKey,TValue> 含む空の文字列を作成し、 メソッドを Add 使用していくつかの要素を追加します。 この例では、重複するキーを Add 追加しようとしたときに メソッドが を ArgumentException スローすることを示します。

このコード例は、SortedDictionary<TKey,TValue> クラスのために提供されている大規模な例の一部です。

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

注釈

内のすべてのキーは、既定の SortedDictionary<TKey,TValue> 比較子に従って一意である必要があります。

SortedDictionary<TKey,TValue> キー比較を実行するには、比較子の実装が必要です。 このコンストラクターは、既定のジェネリック等価比較子 Comparer<T>.Defaultを使用します。 type TKey がジェネリック インターフェイスを実装する System.IComparable<T> 場合、既定の比較子はその実装を使用します。 または、 パラメーターを受け取るコンストラクターを IComparer<T> 使用して、ジェネリック インターフェイスの実装を comparer 指定することもできます。

このコンストラクターは O(1) 操作です。

こちらもご覧ください

適用対象

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

空で、指定した SortedDictionary<TKey,TValue> を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

パラメーター

comparer
IComparer<TKey>

キーの比較時に使用する IComparer<T> 実装。キーの型の既定の null を使用する場合は Comparer<T>

次のコード例では、現在のカルチャの大文字と小文字を区別しない比較子を使用して を作成 SortedDictionary<TKey,TValue> します。 この例では、4 つの要素を追加します。一部は小文字のキーを持ち、一部は大文字のキーを使用します。 次に、既存のキーと異なるキーを持つ要素をケースによってのみ追加し、結果の例外をキャッチし、エラー メッセージを表示します。 最後に、大文字と小文字を区別しない並べ替え順序で要素を表示します。

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

注釈

SortedDictionary<TKey,TValue> のすべてのキーは、指定された比較子に従って一意である必要があります。

SortedDictionary<TKey,TValue> キー比較を実行するには、比較子の実装が必要です。 が nullの場合comparer、このコンストラクターは既定のジェネリック等価比較子 Comparer<T>.Defaultを使用します。 type TKey がジェネリック インターフェイスを実装する System.IComparable<T> 場合、既定の比較子はその実装を使用します。

このコンストラクターは O(1) 操作です。

こちらもご覧ください

適用対象

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、キーの型の既定の IDictionary<TKey,TValue> 実装を使用する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

パラメーター

dictionary
IDictionary<TKey,TValue>

新しい IDictionary<TKey,TValue> に要素がコピーされた SortedDictionary<TKey,TValue>

例外

dictionarynullです。

dictionary には 1 つまたは複数の重複するキーが含まれます。

次のコード例では、 を使用 SortedDictionary<TKey,TValue> して、 内の情報 Dictionary<TKey,TValue>の並べ替えられたコピーを作成し、 をコンストラクターに渡 Dictionary<TKey,TValue> す方法を SortedDictionary<TKey,TValue>(IComparer<TKey>) 示します。

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

注釈

SortedDictionary<TKey,TValue> のすべてのキーは、既定の比較子に従って一意である必要があります。したがって、ソース dictionary 内のすべてのキーも、既定の比較子に従って一意である必要があります。

SortedDictionary<TKey,TValue> キー比較を実行するには、比較子の実装が必要です。 このコンストラクターでは、 Comparer<T>.Default既定のジェネリック等価比較子 を使用します。 type TKey がジェネリック インターフェイスを実装する System.IComparable<T> 場合、既定の比較子はその実装を使用します。 または、 パラメーターを受け取るコンストラクターを IComparer<T> 使用して、ジェネリック インターフェイスの実装を comparer 指定することもできます。

このコンストラクターは O(n log n) 操作です。ここで n 、 は 内 dictionaryの要素の数です。

こちらもご覧ください

適用対象

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

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

指定した SortedDictionary<TKey,TValue> から要素をコピーして格納し、指定した IDictionary<TKey,TValue> 実装を使用してキーを比較する、IComparer<T> クラスの新しいインスタンスを初期化します。

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

パラメーター

dictionary
IDictionary<TKey,TValue>

新しい IDictionary<TKey,TValue> に要素がコピーされた SortedDictionary<TKey,TValue>

comparer
IComparer<TKey>

キーの比較時に使用する IComparer<T> 実装。キーの型の既定の null を使用する場合は Comparer<T>

例外

dictionarynullです。

dictionary には 1 つまたは複数の重複するキーが含まれます。

次のコード例では、 を使用SortedDictionary<TKey,TValue>して、 をコンストラクターに渡Dictionary<TKey,TValue>すことによって、大文字と小文字を区別しない並べ替えられた情報のコピーを、大文字と小文字を区別せずにDictionary<TKey,TValue>作成する方法をSortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)示します。 この例では、大文字と小文字を区別しない比較子は現在のカルチャ用です。

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

注釈

SortedDictionary<TKey,TValue> のすべてのキーは、指定された比較子に従って一意である必要があります。したがって、ソース dictionary 内のすべてのキーも、指定された比較子に従って一意である必要があります。

SortedDictionary<TKey,TValue> キー比較を実行するには、比較子の実装が必要です。 が nullの場合comparer、このコンストラクターは既定のジェネリック等価比較子 Comparer<T>.Defaultを使用します。 type TKey がジェネリック インターフェイスを実装する System.IComparable<T> 場合、既定の比較子はその実装を使用します。

このコンストラクターは O(n log n) 操作です。ここで n 、 は 内 dictionaryの要素の数です。

こちらもご覧ください

適用対象