VB.NET HashMap 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5244991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:20:06  来源:igfitidea点击:

VB.NET HashMap equivalent

vb.nethashmap

提问by Andrew

I'm trying to store a set of objects and I need to be able to access them in constant time based on a particular property of the objects. I was hoping to do this by adding the objects to a HashMap and using the property that I want to index by as the key. Is there a HashMap object in VB like in Java, or should I use something else?

我正在尝试存储一组对象,并且我需要能够根据对象的特定属性在恒定时间内访问它们。我希望通过将对象添加到 HashMap 并使用我想要索引的属性作为键来做到这一点。在 VB 中是否有一个 HashMap 对象像在 Java 中一样,或者我应该使用其他东西吗?

Update: Using VB 2010, .NET 4

更新:使用 VB 2010、.NET 4

Cheers

干杯

回答by Morvader

Depending on your needs you could use a HashTableor a Dictionary.

根据您的需要,您可以使用HashTableDictionary

like this:

像这样:

Dim dictionary As New Dictionary(Of String, Integer)
dictionary.Add("Dot", 20)
dictionary.Add("Net", 1)
dictionary.Add("Perls", 10)
dictionary.Add("Visual", -1)

Dim Hashtable As New Hashtable()
hashtable.Add("Area", 1000)
hashtable.Add("Perimeter", 55)
hashtable.Add("Mortgage", 540)

Have a look at thisand thisfor more usage examples.

看看这个这个更多的使用示例。

UPDATE:

更新

But, as @Konrad Rudolph says, its better to use a Dictionaryfor multiple reasons. (On .NET 2.0 and obove)

但是,正如@Konrad Rudolph 所说,Dictionary出于多种原因最好使用 a 。(在 .NET 2.0 及以上)

Thanks for the comment!

感谢您的评论!