次の方法で共有


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();
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# 言語の foreach ステートメント (Visual Basic でFor Each ) は、コレクション内の要素の型のオブジェクトを返します。 IDictionary<TKey,TValue>に基づくコレクションの各要素はキーと値のペアであるため、要素の型はキーの型または値の型ではありません。 代わりに、要素の型は KeyValuePair<TKey,TValue>。 例えば次が挙げられます。

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>の文字列形式を返します。

適用対象

こちらもご覧ください