SortedDictionary コンストラクタ ()
アセンブリ: System (system.dll 内)


SortedDictionary 内のすべてのキーは既定の比較演算子に従って一意である必要があります。
SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。代わりに、comparer パラメータを受け取るコンストラクタを使用して、IComparer ジェネリック インターフェイスの実装を指定することもできます。

文字列キーを含む文字列の空の SortedDictionary を作成し、Add メソッドを使用していくつかの要素を追加するコード例を次に示します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。
このコード例は、SortedDictionary クラスのトピックで取り上げているコード例の一部分です。
' 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
// 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."); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedDictionary コンストラクタ

名前 | 説明 |
---|---|
SortedDictionary () | 空で、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。 |
SortedDictionary (ジェネリック IComparer) | 空で、指定した IComparer を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。 |
SortedDictionary (ジェネリック IDictionary) | 指定した IDictionary から要素をコピーして格納し、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。 |
SortedDictionary (ジェネリック IDictionary, ジェネリック IComparer) | 指定した IDictionary から要素をコピーして格納し、指定した IComparer 実装を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。 |

関連項目
SortedDictionary ジェネリック クラスSortedDictionary メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedDictionary コンストラクタ (ジェネリック IDictionary)
アセンブリ: System (system.dll 内)

Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim instance As New SortedDictionary(Of TKey, TValue)(dictionary)
public SortedDictionary ( IDictionary<TKey,TValue> dictionary )
public: SortedDictionary ( IDictionary<TKey, TValue>^ dictionary )
public SortedDictionary ( IDictionary<TKey,TValue> dictionary )
public function SortedDictionary ( dictionary : IDictionary<TKey,TValue> )

例外の種類 | 条件 |
---|---|
ArgumentNullException | dictionary が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException |

SortedDictionary 内のすべてのキーは、既定の比較演算子に従って一意である必要があります。したがって、元の dictionary 内のすべてのキーも、既定の比較演算子に従って一意である必要があります。
SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。代わりに、comparer パラメータを受け取るコンストラクタを使用して、IComparer ジェネリック インターフェイスの実装を指定することもできます。

Dictionary を SortedDictionary(ジェネリック IComparer) コンストラクタに渡すことにより、SortedDictionary を使用して、Dictionary 内の情報の並べ替えられたコピーを作成する方法を次のコード例に示します。
Imports System 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
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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
IDictionary ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedDictionary コンストラクタ (ジェネリック IComparer)
アセンブリ: System (system.dll 内)


SortedDictionary 内のすべてのキーは指定した比較演算子に従って一意である必要があります。
SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。

現在のカルチャの大文字小文字を区別しない比較演算子を使って SortedDictionary を作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、大文字小文字を区別しない並べ替え順で要素を表示します。
Imports System 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
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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
IComparer ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedDictionary コンストラクタ (ジェネリック IDictionary, ジェネリック IComparer)
アセンブリ: System (system.dll 内)

Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue), _ comparer As IComparer(Of TKey) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim comparer As IComparer(Of TKey) Dim instance As New SortedDictionary(Of TKey, TValue)(dictionary, comparer)
public SortedDictionary ( IDictionary<TKey,TValue> dictionary, IComparer<TKey> comparer )
public: SortedDictionary ( IDictionary<TKey, TValue>^ dictionary, IComparer<TKey>^ comparer )
public SortedDictionary ( IDictionary<TKey,TValue> dictionary, IComparer<TKey> comparer )
public function SortedDictionary ( dictionary : IDictionary<TKey,TValue>, comparer : IComparer<TKey> )

例外の種類 | 条件 |
---|---|
ArgumentNullException | dictionary が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException |

SortedDictionary 内のすべてのキーは、指定した比較演算子に従って一意である必要があります。したがって、元の dictionary 内のすべてのキーも、指定した比較演算子に従って一意である必要があります。
SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。

Dictionary を SortedDictionary(ジェネリック IDictionary,ジェネリック IComparer) コンストラクタに渡すことにより、SortedDictionary を使用して、大文字小文字を区別しない Dictionary 内の情報の、大文字小文字を区別せずに並べ替えられたコピーを作成する方法を次のコード例に示します。この例では、現在のカルチャの大文字小文字を区別しない比較演算子が使用されます。
Imports System 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
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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
IDictionary ジェネリック インターフェイス
IComparer ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
- SortedDictionary コンストラクタ ()のページへのリンク