VB.NET KeyValuePair Examples

Use KeyValuePair, which has two member fields. Access the Key and Value properties.

KeyValuePair. A KeyValuePair is a Structure. It has two fields of specified types. It stores two pieces of data together as a single object. In VB.NET it is useful. It defines a Structure of any kind of key, and any kind of value.

Example. Here we create a List that stores elements of type KeyValuePair. Notice how the (Of String, Integer) syntax is used to specify that the key of the pair is a String, and the value is an Integer.ListStringsInteger

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 Each, For
VB.NET program that creates List of KeyValuePair instances 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)("dot", 1)) list.Add(New KeyValuePair(Of String, Integer)("net", 2)) list.Add(New KeyValuePair(Of String, Integer)("perls", 3)) ' Loop over pairs. For Each pair As KeyValuePair(Of String, Integer) In list ' Get key. Dim key As String = pair.Key ' Get value. Dim value As Integer = pair.Value ' Display. Console.WriteLine("{0}, {1}", key, value) Next End Sub End Module Output dot, 1 net, 2 perls, 3

Example 2. Sometimes you may want to return two values at once from a function in your VB.NET program. You could use the ByRef modifier. But the KeyValuePair can also be returned. The Key and Value can store the two values.

Here: In this example, the GetPair Function returns a new instance of the KeyValuePair type that has a specific key and value.

VB.NET program that returns KeyValuePair from Function Module Module1 Sub Main() Dim pair As KeyValuePair(Of Integer, Integer) = GetPair() Console.WriteLine(pair.Key) Console.WriteLine(pair.Value) End Sub Function GetPair() As KeyValuePair(Of Integer, Integer) ' Create new pair. Dim pair As KeyValuePair(Of Integer, Integer) = New KeyValuePair(Of Integer, Integer)(5, 8) ' Return the pair. Return pair End Function End Module Output 5 8

Tuple. The KeyValuePair is restricted to a key and a value, but the Tuple type can have more items in its memory. If you need three of more fields grouped together, please 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.


Dictionary. The easiest way to loop over the keys and values in a Dictionary instance is with the For-Each loop construct. In this code, you must use the KeyValuePair type as the type of the enumeration variable in the loop.Dictionary

Summary. KeyValuePair stores a key and a value together in a single object in memory. It is a value type. This makes it more efficient in some cases and less efficient in others. A common usage is in the For-Each loop in a Dictionary.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to