x


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,

more ▼

asked Feb 27 '12 at 03:24 AM

dansav gravatar image

dansav
373 104 131 154

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
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.

more ▼

answered Feb 27 '12 at 05:24 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

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?

Feb 27 '12 at 06:49 AM dansav

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.

Feb 27 '12 at 07:32 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x57
x53

asked: Feb 27 '12 at 03:24 AM

Seen: 2878 times

Last Updated: Feb 27 '12 at 07:32 AM