.NET Array Class File String Sub ArrayList Cast Console DataTable DateTime Dictionary Enum For If IndexOf Interface Lambda LINQ List Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder Substring

KeyValuePair: Key and Value properties

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.


One: 1

In this first example, we create a List that stores element 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.

Note:When creating a KeyValuePair, you must set the key and the value in the constructor.

And:Finally in this example, we loop through all the elements of the list with For-Each.

For Each
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)
    End Sub
End Module


dot, 1
net, 2
perls, 3

Example 2

Return keyword

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()
    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




Tuple: type name

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.



Foreach loop construct

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.

And:For more detail please visit the core Dictionary article on this site. It has many examples.



The VB.NET programming language

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.