設定または取得できる、キー/値ペアを定義します。
アセンブリ: mscorlib (mscorlib.dll 内)
<SerializableAttribute> _ Public Structure KeyValuePair(Of TKey, TValue)
[SerializableAttribute] public struct KeyValuePair<TKey, TValue>
[SerializableAttribute] generic<typename TKey, typename TValue> public value class KeyValuePair
[<Sealed>] [<SerializableAttribute>] type KeyValuePair<'TKey, 'TValue> = struct end
型パラメーター
- TKey
-
キーの型。
- TValue
-
値の型。
KeyValuePair<TKey, TValue> 型で公開されるメンバーは以下のとおりです。
| 名前 | 説明 | |
|---|---|---|
|
KeyValuePair<TKey, TValue> | 指定したキーと値を使用して、KeyValuePair<TKey, TValue> 構造体の新しいインスタンスを初期化します。 |
| 名前 | 説明 | |
|---|---|---|
|
Key | キー/値ペア内のキーを取得します。 |
|
Value | キー/値ペア内の値を取得します。 |
| 名前 | 説明 | |
|---|---|---|
|
Equals | このインスタンスと指定したオブジェクトが等しいかどうかを示します。 (ValueType から継承されます。) |
|
Finalize | オブジェクトがガベジ コレクションにより収集される前に、そのオブジェクトがリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
|
GetHashCode | このインスタンスのハッシュ コードを返します。 (ValueType から継承されます。) |
|
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
|
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
|
ToString | キーと値の文字列形式を使用して、KeyValuePair<TKey, TValue> の文字列形式を返します。 (ValueType.ToString() をオーバーライドします。) |
Dictionary<TKey, TValue>.Enumerator.Current プロパティは、この型のインスタンスを返します。
C# 言語の foreach ステートメント (C++ の場合は for each、Visual Basic の場合は For Each) には、コレクション内の要素の型が必要です。 IDictionary<TKey, TValue> に基づくコレクションの各要素はキー/値ペアであるため、要素の型は、キーの型や値の型にはなりません。 その代わり、要素の型は KeyValuePair<TKey, TValue> になります。 次に例を示します
For Each kvp As KeyValuePair(Of String, String) In myDictionary Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value) Next kvp
foreach( KeyValuePair<string, string> kvp in myDictionary ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
for each(KeyValuePair<String^, String^> kvp in myDictionary) { Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可し、コレクションへの書き込みを防ぎます。
KeyValuePair<TKey, TValue> メソッドを使用してディクショナリのキーと値を列挙する方法を次のコード例に示します。
このコードは、Dictionary<TKey, TValue> クラスのトピックで取り上げているコード例の一部分です。
' When you use foreach to enumerate dictionary elements, ' the elements are retrieved as KeyValuePair objects. Console.WriteLine() For Each kvp As KeyValuePair(Of String, String) In openWith Console.WriteLine("Key = {0}, Value = {1}", _ kvp.Key, kvp.Value) Next kvp
// When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach( KeyValuePair<string, string> kvp in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
// When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console::WriteLine(); for each( KeyValuePair<String^, String^> kvp in openWith ) { Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
.NET Framework
サポート対象: 4、3.5、3.0、2.0.NET Framework Client Profile
サポート対象: 4、3.5 SP1サポート対象:
Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2
.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。