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<string, string>();
myHash.Add(“propertyname”, “something”);

or can I do
openWith[“txt”]=“notepad.exe”);

Thanks,

1 Answer

1

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 instead 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.