- Home /
Dictionary vs Hashtable
I'm trying to use Dictionary instead of Hashtable. I'm having trouble finding any examples of how I would use this in unity javascript. Previously I would do this
myHash=new Hashtable();
myHash["propertyname"]="something";
How would I do this using Dictionary? var myHash = new Dictionary(); myHash.Add("propertyname", "something");
or can I do openWith["txt"]="notepad.exe");
Thanks,
Answer by Eric5h5 · Feb 27, 2012 at 05:24 AM
var myHash = new Dictionary.<String, String>();
myHash.Add ("propertyname", "something");
// or
myHash["propertyname"] = "something";
The difference is that Add won't add a property that already exists; the other way overwrites it.
key,value both have to be strings in this example.
In the unity hashtable the value part could be any type -- I thought that's why it was useful.
Is there some way to make the value be anything you want ie. int, string, array? using Dictionary ins$$anonymous$$d of Hashtable?
You can use type Object to have the value be any type, though you have to cast it to the correct type when retrieving it.