Home
Map
KeyValuePair ExamplesUse KeyValuePair, which has two member fields. Access the Key and Value properties.
VB.NET
This page was last reviewed on Dec 7, 2022.
KeyValuePair. A KeyValuePair is a Structure. It has 2 fields of specified types. It stores 2 pieces of data together as a single object.
Shows a keyvaluepairShows a keyvaluepairShows a keyvaluepair
Data types. KeyValuePair defines a Structure of any kind of key, and any kind of value. Often we use Strings and Integers for the types, but any type is possible—reference or value.
First example. Here we create a List that stores KeyValuePairs. The (Of String, Integer) syntax is used to specify that the key is a String, and the value is an Integer.
Note When creating a KeyValuePair, you must set the key and the value in the constructor.
Finally We loop through all the elements of the list with For-Each. We access Key, and Value.
For
Shows a keyvaluepair
Module Module1 Sub Main() ' Create List of key-value pairs. Dim list As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer)) list.Add(New KeyValuePair(Of String, Integer)("Cat", 1)) list.Add(New KeyValuePair(Of String, Integer)("Dog", 2)) list.Add(New KeyValuePair(Of String, Integer)("Rabbit", 4)) ' Loop over pairs. For Each pair As KeyValuePair(Of String, Integer) In list ' Display key and value. Console.WriteLine(pair) Next End Sub End Module
[Cat, 1] [Dog, 2] [Rabbit, 4]
Return values. Sometimes you may want to return 2 values at once from a function. You could use the ByRef modifier. But the KeyValuePair can also be returned.
Info GetPair() returns a new instance of the KeyValuePair type that has a specific key and value.
Shows a keyvaluepair
Module Module1 Sub Main() Dim pair As KeyValuePair(Of String, String) = GetPair() Console.WriteLine(pair.Key) Console.WriteLine(pair.Value) End Sub Function GetPair() As KeyValuePair(Of String, String) ' Create and return a KeyValuePair instance. Return New KeyValuePair(Of String, String)("image/avif", "avif") End Function End Module
image/avif avif
Dictionary for-each. The easiest way to loop over the keys and values in a Dictionary instance is with the For-Each loop construct. In this code, we use the KeyValuePair in the loop.
Dictionary
Shows a keyvaluepair
Module Program Sub Main() ' Create and add values to the Dictionary. Dim animals As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() animals("Cat") = 2 animals("Dog") = 1 ' Loop over dictionary. For Each pair As KeyValuePair(Of String, Integer) In animals Console.WriteLine(pair) Next End Sub End Module
[Cat, 2] [Dog, 1]
Tuple. The KeyValuePair is restricted to a key and a value, but the Tuple type can have more items. If you need 3 fields grouped together, check out the Tuple type.
Note One drawback of the Tuple type is that is must be allocated on the managed heap as an object instance.
However The KeyValuePair is a structure so it can often be allocated in the stack memory.
Tuple
A summary. KeyValuePair stores a key and a value together in a single object in memory. It is a value type. A common usage is in the For-Each loop in a Dictionary.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.