When you use foreach to enumerate dictionary elements, the elements are retrieved as KeyValuePair objects : SortedDictionary « Data Structure « VB.Net






When you use foreach to enumerate dictionary elements, the elements are retrieved as KeyValuePair objects

 

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main() 
        Dim openWith As New SortedDictionary(Of String, String)
        openWith.Add("A", "a")
        openWith.Add("B", "b")
        openWith.Add("C", "c")
        openWith.Add("D", "d")

        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

   
  








Related examples in the same category

1.Create a new sorted dictionary of strings, with string keys
2.The Add method throws an exception if the new key is already in the dictionary
3.The Item property is the default property, so you can omit its name when accessing elements
4.The default Item property can be used to change the value associated with a key
5.If a key does not exist, setting the default Item property for that key adds a new key/value pair
6.Item property throws an exception if the requested key is not in the dictionary
7.TryGetValue can be a more efficient way to retrieve values
8.ContainsKey can be used to test keys before inserting them
9.To get the values alone, use the Values property
10.To get the keys alone, use the Keys property
11.Use the Remove method to remove a key/value pair
12.StringDictionary Class implements a hash table with the key and the value
13.Display the contents of the collection using the enumerator
14.Uses the Keys, Values, Count, and Item properties
15.Copies the StringDictionary to an array with DictionaryEntry elements
16.Searches for a value
17.Searches for a key and deletes it
18.StringDictionary.Add adds an entry into the StringDictionary.
19.StringDictionary.ContainsKey Method determines if the StringDictionary contains a specific key.
20.StringDictionary.CopyTo Method copies dictionary values to a one-dimensional Array
21.StringDictionary.Count gets the number of key/value pairs
22.StringDictionary.Values Property gets a collection of values in the StringDictionary.