Dictionary コンストラクタ


関連項目
Dictionary ジェネリック クラスDictionary メンバ
System.Collections.Generic 名前空間
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ (SerializationInfo, StreamingContext)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim info As SerializationInfo Dim context As StreamingContext Dim instance As New Dictionary(Of TKey, TValue)(info, context)
protected Dictionary ( SerializationInfo info, StreamingContext context )
protected: Dictionary ( SerializationInfo^ info, StreamingContext context )
protected Dictionary ( SerializationInfo info, StreamingContext context )


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


Dictionary コンストラクタ (ジェネリック IDictionary)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim instance As New Dictionary(Of TKey, TValue)(dictionary)
public Dictionary ( IDictionary<TKey,TValue> dictionary )
public: Dictionary ( IDictionary<TKey, TValue>^ dictionary )
public Dictionary ( IDictionary<TKey,TValue> dictionary )
public function Dictionary ( dictionary : IDictionary<TKey,TValue> )
- dictionary
新しい Dictionary に要素がコピーされた IDictionary。

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

Dictionary 内のすべてのキーは、既定の等値比較演算子に従って一意である必要があります。同様に、元の dictionary 内のすべてのキーも、既定の等値比較演算子に従って一意である必要があります。
新しい Dictionary の初期量は、dictionary 内のすべての要素を格納できるサイズです。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。代わりに、comparer パラメータを受け付けるコンストラクタを使用して、IEqualityComparer ジェネリック インターフェイスの実装を指定することもできます。
このコンストラクタは O(n) 操作です (n は dictionary 内の要素数)。

Dictionary(ジェネリック IEqualityComparer) コンストラクタを使用し、並べ替えられた別のディクショナリの内容によって Dictionary を初期化する方法を次のコード例に示します。このコード例では、SortedDictionary を作成し、このディクショナリにランダムな順序でデータを読み込みます。次に、SortedDictionary を Dictionary(ジェネリック IEqualityComparer) コンストラクタに渡して、並べ替えられた Dictionary を作成します。これは、特定のポイントで静的になる並べ替えられたディクショナリを構築することが必要である場合に有用です。データを SortedDictionary から Dictionary にコピーすることにより、検索速度が向上します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new sorted dictionary of strings, with string ' keys. Dim openWith As New SortedDictionary(Of String, String) ' Add some elements to the sorted dictionary. openWith.Add("txt", "notepad.exe") openWith.Add("bmp", "paint.exe") openWith.Add("dib", "paint.exe") openWith.Add("rtf", "wordpad.exe") ' Create a Dictionary of strings with string keys, and ' initialize it with the contents of the sorted dictionary. Dim copy As New Dictionary(Of String, String)(openWith) ' List the 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 sorted dictionary of strings, with string // keys. SortedDictionary<string, string> openWith = new SortedDictionary<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 Dictionary of strings with string keys, and // initialize it with the contents of the sorted dictionary. Dictionary<string, string> copy = new Dictionary<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 CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
IDictionary
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ (ジェネリック IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim comparer As IEqualityComparer(Of TKey) Dim instance As New Dictionary(Of TKey, TValue)(comparer)
public Dictionary ( IEqualityComparer<TKey> comparer )
public: Dictionary ( IEqualityComparer<TKey>^ comparer )
public Dictionary ( IEqualityComparer<TKey> comparer )
public function Dictionary ( comparer : IEqualityComparer<TKey> )

このコンストラクタを、StringComparer クラスによって提供される大文字と小文字を区別しない文字列比較演算子に使用して、大文字と小文字を区別しない文字列キーを持つディクショナリを作成します。
Dictionary 内のすべてのキーは指定した比較演算子に従って一意である必要があります。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。
![]() |
---|
コレクションのサイズを推定できる場合は、初期量を指定するコンストラクタを使用すると、Dictionary に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。 |

現在のカルチャの大文字小文字を区別しない等値比較演算子を使って Dictionary を作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、ディクショナリの要素を表示します。
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 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") ' 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 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 = txt, Value = notepad.exe 'Key = bmp, Value = paint.exe 'Key = DIB, Value = paint.exe 'Key = rtf, Value = wordpad.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 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"); // 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 = txt, Value = notepad.exe Key = bmp, Value = paint.exe Key = DIB, Value = paint.exe Key = rtf, Value = wordpad.exe */

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


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
IEqualityComparer
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ (ジェネリック IDictionary, ジェネリック IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue), _ comparer As IEqualityComparer(Of TKey) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim comparer As IEqualityComparer(Of TKey) Dim instance As New Dictionary(Of TKey, TValue)(dictionary, comparer)
public Dictionary ( IDictionary<TKey,TValue> dictionary, IEqualityComparer<TKey> comparer )
public: Dictionary ( IDictionary<TKey, TValue>^ dictionary, IEqualityComparer<TKey>^ comparer )
public Dictionary ( IDictionary<TKey,TValue> dictionary, IEqualityComparer<TKey> comparer )
public function Dictionary ( dictionary : IDictionary<TKey,TValue>, comparer : IEqualityComparer<TKey> )
- dictionary
新しい Dictionary に要素がコピーされた IDictionary。

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

このコンストラクタを、StringComparer クラスによって提供される大文字と小文字を区別しない文字列比較演算子に使用して、大文字と小文字を区別しない文字列キーを持つディクショナリを作成します。
Dictionary 内のすべてのキーは、指定した比較演算子に従って一意である必要があります。同様に、元の dictionary 内のすべてのキーも、指定した比較演算子に従って一意である必要があります。
![]() |
---|
たとえば、comparer が StringComparer クラスによって提供される大文字と小文字を区別しない文字列比較演算子の 1 つであり、dictionary が大文字と小文字を区別しない比較演算子キーを使用しない場合、キーが重複する可能性があります。 |
新しい Dictionary の初期量は、dictionary 内のすべての要素を格納できるサイズです。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。
このコンストラクタは O(n) 操作です (n は dictionary 内の要素数)。

Dictionary(ジェネリック IDictionary,ジェネリック IEqualityComparer) コンストラクタを使用し、大文字小文字を区別しないで並べ替えられた別のディクショナリの内容によって Dictionary を初期化する方法を次のコード例に示します。このコード例では、大文字小文字を区別しない比較演算子を使って SortedDictionary を作成し、このディクショナリにランダムな順序でデータを読み込みます。次に、大文字小文字を区別しない等値比較演算子と共に SortedDictionary を Dictionary(ジェネリック IDictionary,ジェネリック IEqualityComparer) コンストラクタに渡して、並べ替えられた Dictionary を作成します。これは、特定のポイントで静的になる並べ替えられたディクショナリを構築することが必要である場合に有用です。データを SortedDictionary から Dictionary にコピーすることにより、検索速度が向上します。
![]() |
---|
この例のように、大文字と小文字を区別しない比較演算子を使用する新しいディクショナリを作成し、それに大文字と小文字を区別する比較演算子を使用するディクショナリのエントリを読み込む場合、入力ディクショナリに大文字と小文字が違うだけのキーがあると例外が発生します。 |
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new sorted dictionary of strings, with string ' keys and a case-insensitive comparer. Dim openWith As New SortedDictionary(Of String, String)( _ StringComparer.CurrentCultureIgnoreCase) ' Add some elements to the sorted dictionary. openWith.Add("txt", "notepad.exe") openWith.Add("Bmp", "paint.exe") openWith.Add("DIB", "paint.exe") openWith.Add("rtf", "wordpad.exe") ' Create a Dictionary of strings with string keys and a ' case-insensitive equality comparer, and initialize it ' with the contents of the sorted dictionary. Dim copy As New Dictionary(Of String, String)(openWith, _ StringComparer.CurrentCultureIgnoreCase) ' List the 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 sorted dictionary of strings, with string // keys and a case-insensitive comparer. 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"); // Create a Dictionary of strings with string keys and a // case-insensitive equality comparer, and initialize it // with the contents of the sorted dictionary. Dictionary<string, string> copy = new Dictionary<string, string>(openWith, StringComparer.CurrentCultureIgnoreCase); // 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 CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
IDictionary
IEqualityComparer
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ (Int32, ジェネリック IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim capacity As Integer Dim comparer As IEqualityComparer(Of TKey) Dim instance As New Dictionary(Of TKey, TValue)(capacity, comparer)
public Dictionary ( int capacity, IEqualityComparer<TKey> comparer )
public: Dictionary ( int capacity, IEqualityComparer<TKey>^ comparer )
public Dictionary ( int capacity, IEqualityComparer<TKey> comparer )


このコンストラクタを、StringComparer クラスによって提供される大文字と小文字を区別しない文字列比較演算子に使用して、大文字と小文字を区別しない文字列キーを持つディクショナリを作成します。
Dictionary 内のすべてのキーは指定した比較演算子に従って一意である必要があります。
Dictionary の容量は、サイズ変更が必要になる前に Dictionary に追加できる要素の数です。Dictionary に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Dictionary に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。

初期量 5 で、現在のカルチャの大文字小文字を区別しない等値比較演算子を使って Dictionary を作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、ディクショナリの要素を表示します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new Dictionary of strings, with string keys, an ' initial capacity of 5, and a case-insensitive equality ' comparer. Dim openWith As New Dictionary(Of String, String)(5, _ StringComparer.CurrentCultureIgnoreCase) ' Add 4 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 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 = txt, Value = notepad.exe 'Key = bmp, Value = paint.exe 'Key = DIB, Value = paint.exe 'Key = rtf, Value = wordpad.exe
using System; using System.Collections.Generic; public class Example { public static void Main() { // Create a new dictionary of strings, with string keys, an // initial capacity of 5, and a case-insensitive equality // comparer. Dictionary<string, string> openWith = new Dictionary<string, string>(5, StringComparer.CurrentCultureIgnoreCase); // Add 4 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 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 = txt, Value = notepad.exe Key = bmp, Value = paint.exe Key = DIB, Value = paint.exe Key = rtf, Value = wordpad.exe */

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


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
IEqualityComparer
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

public: Dictionary ( int capacity )


Dictionary 内のすべてのキーは既定の等値比較演算子に従って一意である必要があります。
Dictionary の容量は、サイズ変更が必要になる前に Dictionary に追加できる要素の数です。Dictionary に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Dictionary に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。代わりに、comparer パラメータを受け付けるコンストラクタを使用して、IEqualityComparer ジェネリック インターフェイスの実装を指定することもできます。

ディクショナリを初期量 4 で作成し、4 つのエントリを設定するコード例を次に示します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new dictionary of strings, with string keys and ' an initial capacity of 4. Dim openWith As New Dictionary(Of String, String)(4) ' Add 4 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 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
using System; using System.Collections.Generic; public class Example { public static void Main() { // Create a new dictionary of strings, with string keys and // an initial capacity of 4. Dictionary<string, string> openWith = new Dictionary<string, string>(4); // Add 4 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); } } } /* 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 */

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


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Dictionary コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As New Dictionary(Of TKey, TValue)

Dictionary 内のすべてのキーは既定の等値比較演算子に従って一意である必要があります。
Dictionary は、キーが同じであるかどうかを確認するための等値比較の実装を必要とします。このコンストラクタは、既定のジェネリック等値比較演算子である EqualityComparer.Default を使用します。型 TKey が System.IEquatable ジェネリック インターフェイスを実装している場合は、既定の等値比較演算子でその実装が使用されます。代わりに、comparer パラメータを受け付けるコンストラクタを使用して、IEqualityComparer ジェネリック インターフェイスの実装を指定することもできます。
![]() |
---|
コレクションのサイズを推定できる場合は、初期量を指定するコンストラクタを使用すると、Dictionary に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。 |

文字列キーを含む文字列の空の Dictionary を作成し、Add メソッドを使用していくつかの要素を追加するコード例を次に示します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。
このコード例は、Dictionary クラスのトピックで取り上げているコード例の一部分です。
' Create a new dictionary of strings, with string keys. ' Dim openWith As New Dictionary(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 dictionary of strings, with string keys. // Dictionary<string, string> openWith = new Dictionary<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 CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Dictionary ジェネリック クラス
Dictionary メンバ
System.Collections.Generic 名前空間
EqualityComparer.Default
IEquatable ジェネリック インターフェイス
Weblioに収録されているすべての辞書からDictionary コンストラクタを検索する場合は、下記のリンクをクリックしてください。

- Dictionary コンストラクタのページへのリンク