vb.net dictionary用法

Dictionary 是一个泛型类,用于在 .NET Framework 中存储键/值对。它是 Hashtable 的替代品,具有更快的查找速度和更强的类型安全性。

下面是如何使用 Dictionary 的一个示例:

' 定义 Dictionary 变量
Dim myDictionary As New Dictionary(Of String, Integer)
' 向字典中添加键/值对
myDictionary.Add("Key1", 1)
myDictionary.Add("Key2", 2)
myDictionary.Add("Key3", 3)
' 访问字典中的值
Dim value1 As Integer = myDictionary("Key1")
Dim value2 As Integer = myDictionary("Key2")
Dim value3 As Integer = myDictionary("Key3")
' 枚举字典中的键/值对
For Each pair As KeyValuePair(Of String, Integer) In myDictionary
    Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value)

这就是如何使用 VB.NET 中的 Dictionary 的简单介绍。

  • 5年前
  •