KeyValuePair<TKey,TValue> 構造体
定義
設定または取得できる、キー/値ペアを定義します。Defines a key/value pair that can be set or retrieved.
generic <typename TKey, typename TValue>
public value class KeyValuePair
public 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
キーの型。The type of the key.
- TValue
値の型。The type of the value.
- 継承
- 属性
例
次のコード例は、構造体を使用して、ディクショナリ内のキーと値を列挙する方法を示して KeyValuePair<TKey,TValue> います。The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.
このコードは、クラスに用意されている大規模な例の一部です Dictionary<TKey,TValue> 。This code is part of a larger example provided for the Dictionary<TKey,TValue> class.
// 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 この型のインスタンスを返します。The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
foreach
C# 言語のステートメント ( for each
C++ では For Each
Visual Basic) は、コレクション内の要素の型のオブジェクトを返します。The foreach
statement of the C# language (for each
in C++, For Each
in Visual Basic) returns an object of the type of the elements in the collection. に基づくコレクションの各要素 IDictionary<TKey,TValue> はキーと値のペアであるため、要素の型はキーの型でも、値の型でもありません。Since each element of a collection based on IDictionary<TKey,TValue> is a key/value pair, the element type is not the type of the key or the type of the value. 代わりに、要素の型は KeyValuePair<TKey,TValue> です。Instead, the element type is KeyValuePair<TKey,TValue>. 次に例を示します。For example:
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
ステートメントは、列挙子のラッパーであり、コレクションへの書き込みではなく読み取りのみを許可します。The foreach
statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
コンストラクター
KeyValuePair<TKey,TValue>(TKey, TValue) |
指定したキーと値を使用して、KeyValuePair<TKey,TValue> 構造体の新しいインスタンスを初期化します。Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
プロパティ
Key |
キー/値ペア内のキーを取得します。Gets the key in the key/value pair. |
Value |
キー/値ペア内の値を取得します。Gets the value in the key/value pair. |
メソッド
Deconstruct(TKey, TValue) |
現在の KeyValuePair<TKey,TValue> を分解します。Deconstructs the current KeyValuePair<TKey,TValue>. |
ToString() |
キーと値の文字列形式を使用して、KeyValuePair<TKey,TValue> の文字列形式を返します。Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |