Coming from a Java world into a C# one is there a HashMap equivalent? If not what would you recommend?
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Dictionary
is probably the closest. System.Collections.Generic.Dictionary
implements the System.Collections.Generic.IDictionary
interface (which is similar to Java's Map
interface).
Some notable differences that you should be aware of:
put
and get
methods for setting/getting items myMap.put(key, value)
MyObject value = myMap.get(key)
[]
indexing for setting/getting items myDictionary[key] = value
MyObject value = myDictionary[key]
null
keys HashMap
allows null keysDictionary
throws an ArgumentNullException
if you try to add a null keyHashMap
will replace the existing value with the new one.Dictionary
will replace the existing value with the new one if you use []
indexing. If you use the Add
method, it will instead throw an ArgumentException
.HashMap
will return null.Dictionary
will throw a KeyNotFoundException
. You can use the TryGetValue
method instead of the []
indexing to avoid this:MyObject value = null; if (!myDictionary.TryGetValue(key, out value)) { /* key doesn't exist */ }
Dictionary
's has a ContainsKey
method that can help deal with the previous two problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With