KeyValuePair<TKey,TValue> 構造体

定義

設定または取得できる、キー/値ペアを定義します。

generic <typename TKey, typename TValue>
public value class KeyValuePair
public struct KeyValuePair<TKey,TValue>
public readonly struct KeyValuePair<TKey,TValue>
[System.Serializable]
public struct KeyValuePair<TKey,TValue>
type KeyValuePair<'Key, 'Value> = struct
[<System.Serializable>]
type KeyValuePair<'Key, 'Value> = struct
Public Structure KeyValuePair(Of TKey, TValue)

型パラメーター

TKey

キーの型。

TValue

値の型。

継承
KeyValuePair<TKey,TValue>
属性

次のコード例は、 構造体を使用してディクショナリ内のキーと値を列挙する方法を 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( 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();
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 kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp

注釈

プロパティは Dictionary<TKey,TValue>.Enumerator.Current 、この型のインスタンスを返します。

C# 言語の ステートメント (for eachVisual Basic では C++) はforeachFor Eachコレクション内の要素の型のオブジェクトを返します。 に IDictionary<TKey,TValue> 基づくコレクションの各要素はキーと値のペアであるため、要素の型はキーの型または値の型ではありません。 代わりに、要素の型は です KeyValuePair<TKey,TValue>。 次に例を示します。

for each(KeyValuePair<String^, String^> kvp in myDictionary)
{
    Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of String, String) In myDictionary
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp

ステートメントは foreach 列挙子を囲むラッパーであり、コレクションからの読み取りのみを許可し、コレクションへの書き込みは許可しません。

コンストラクター

KeyValuePair<TKey,TValue>(TKey, TValue)

指定したキーと値を使用して、KeyValuePair<TKey,TValue> 構造体の新しいインスタンスを初期化します。

プロパティ

Key

キー/値ペア内のキーを取得します。

Value

キー/値ペア内の値を取得します。

メソッド

Deconstruct(TKey, TValue)

現在の KeyValuePair<TKey,TValue> を分解します。

ToString()

キーと値の文字列形式を使用して、KeyValuePair<TKey,TValue> の文字列形式を返します。

適用対象

こちらもご覧ください