Reputation: 3195
Is it possible to fill a hashtable when it is first defined?
For example, here is some similar PHP code:
var $AA = new array("one" => 1, "two" => 2);
I would like to do something like this in C#. In fact, it is quite possible Hashtable is not the best tool.
take care, lee
Upvotes: 1
Views: 191
Reputation: 8620
Look into the collection initializers - yes, it's possible. For dictionaries, see here. You can declare your dictionary and fill it with a collection of key/value pairs.
A simpler example than MSDN provides:
var MyDictionary = new Dictionary<string,int>()
{
{"one", 1},
{"two", 2}
};
Upvotes: 6