下面的示例演示如何使用 DictionaryEntry 来循环访问 Hashtable 对象。
'A simple example for the DictionaryEntry structure.
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Module Example
Sub Main()
' Create a new hash table.
'
Dim openWith As New Hashtable()
' Add some elements to the hash table. There are no
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' When you use For Each to enumerate hash table elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each de As DictionaryEntry In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
de.Key, de.Value)
Next de
End Sub
End Module
' This code example produces output similar to the following:
'
'Key = rtf, Value = winword.exe
'Key = txt, Value = notepad.exe
'Key = dib, Value = paint.exe
'Key = bmp, Value = paint.exe
本文通过一个简单示例展示了如何使用DictionaryEntry结构遍历并访问Hashtable中的元素。示例使用Visual Basic编写,创建了一个包含几种文件扩展名及其关联程序的Hashtable,并通过ForEach循环输出了这些键值对。
1478

被折叠的 条评论
为什么被折叠?



