SortedDictionary<TKey,TValue> コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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>()
空の 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を使用します。 型 TKey が System.IComparable<T> ジェネリック インターフェイスを実装する場合、既定の比較子はその実装を使用します。 または、IComparer<T> パラメーターを受け取るコンストラクターを使用して、comparer ジェネリック インターフェイスの実装を指定することもできます。
このコンストラクターは O(1) 操作です。
こちらもご覧ください
適用対象
SortedDictionary<TKey,TValue>(IComparer<TKey>)
空の SortedDictionary<TKey,TValue> クラスの新しいインスタンスを初期化し、指定した IComparer<T> 実装を使用してキーを比較します。
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> キー比較を実行するには、比較子の実装が必要です。
comparerがnullの場合、このコンストラクターは既定のジェネリック等値比較子 (Comparer<T>.Default) を使用します。 型 TKey が System.IComparable<T> ジェネリック インターフェイスを実装する場合、既定の比較子はその実装を使用します。
このコンストラクターは O(1) 操作です。
こちらもご覧ください
適用対象
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)
指定した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>。
例外
dictionary は nullです。
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) を使用します。 型 TKey が System.IComparable<T> ジェネリック インターフェイスを実装する場合、既定の比較子はその実装を使用します。 または、IComparer<T> パラメーターを受け取るコンストラクターを使用して、comparer ジェネリック インターフェイスの実装を指定することもできます。
このコンストラクターは O(n ログ n) 操作です。ここで、 n は dictionary内の要素の数です。
こちらもご覧ください
適用対象
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
指定した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);
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>。
例外
dictionary は nullです。
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> キー比較を実行するには、比較子の実装が必要です。
comparerがnullの場合、このコンストラクターは既定のジェネリック等値比較子 (Comparer<T>.Default) を使用します。 型 TKey が System.IComparable<T> ジェネリック インターフェイスを実装する場合、既定の比較子はその実装を使用します。
このコンストラクターは O(n ログ n) 操作です。ここで、 n は dictionary内の要素の数です。