獲取字典值
你可以使用 Item
屬性獲取字典中條目的值:
Dim extensions As New Dictionary(Of String, String) From {
{ "txt", "notepad" },
{ "bmp", "paint" },
{ "doc", "winword" }
}
Dim program As String = extensions.Item("txt") 'will be "notepad"
' alternative syntax as Item is the default property (a.k.a. indexer)
Dim program As String = extensions("txt") 'will be "notepad"
' other alternative syntax using the (rare)
' dictionary member access operator (a.k.a. bang operator)
Dim program As String = extensions!txt 'will be "notepad"
如果字典中不存在該鍵,則將丟擲 KeyNotFoundException。