Note
Access to this page requires authorization. You can try signing in or .
Access to this page requires authorization. You can try .
KeyValuePair<TKey,TValue> Struct
Definition
- Namespace:
- System.Collections.Generic
- Assemblies:
- mscorlib.dll, System.Runtime.dll
- Assemblies:
- netstandard.dll, System.Runtime.dll
- Assembly:
- System.Runtime.dll
- Assembly:
- mscorlib.dll
- Assembly:
- netstandard.dll
- Source:
- KeyValuePair.cs
- Source:
- KeyValuePair.cs
- Source:
- KeyValuePair.cs
- Source:
- KeyValuePair.cs
- Source:
- KeyValuePair.cs
Important
Some information relates to prerelease product that may be substantially modified before itβs released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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>
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)
Type Parameters
- TKey
The type of the key.
- TValue
The type of the value.
- Inheritance
- Attributes
Examples
The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.
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();
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
Remarks
The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
The foreach statement of the C# language (For Each in Visual Basic) returns an object of the type of the elements in the collection. 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. Instead, the element type is KeyValuePair<TKey,TValue>. For example:
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
The foreach statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
Constructors
| Name | Description |
|---|---|
| KeyValuePair<TKey,TValue>(TKey, TValue) |
Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
Properties
| Name | Description |
|---|---|
| Key |
Gets the key in the key/value pair. |
| Value |
Gets the value in the key/value pair. |
Methods
| Name | Description |
|---|---|
| Deconstruct(TKey, TValue) |
Deconstructs the current KeyValuePair<TKey,TValue>. |
| ToString() |
Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |
Applies to
See also
Feedback
Was this page helpful?
